What’s new in Java 13

Umashankar Patra
Xebia Engineering Blog
4 min readNov 18, 2019

--

Some of the important Java 13 features are:

  • Text blocks
  • Switch Expressions
  • Uncommit Unused Memory
  • Dynamic CDS Archive
  • Reimplement the legacy Socket API

Feature 1: Text Blocks

It is very difficult to embed different code snippets like HTML, JSON, etc to string literals as we need to add escape sequences, which makes the code less readable. Java 13 introduces a new feature “Text Blocks”. These features are in the preview phase as of now, and hence can be modified in upcoming releases.

Text blocks allow multiline string literals that avoid the need for most of the escape sequences. It allows developers to format the string in a predictable manner, making code cleaner and more readable. Before this feature, it was very difficult to read multiline texts in Java.

Text block is a solution to multi-line text in java.It uses triple quotation marks as delimiters.The delimiter is a sequence of three double quote characters (“””).

//Without Text blocks
String java13FeaturesHtml = “<html>\n” +
“ <body>\n” +“ <p>Java13 features</p>\n” +“ <p>Text Blocks</p>\n” +“ <p>Switch Expressions</p>\n” +“ <p>Reimplement the legacy Socket API</p>\n” +“ <p>Dynamic CDS Archive</p>\n” +“ <p>Uncommit Unused Memory</p>\n” +“ </body>\n” +“</html>\n”;//Using Text blocksString java13FeaturesHtml= “””<html><body><p>Java13 features</p><p>Text Blocks </p><p>Switch Expressions </p><p>Reimplement the legacy Socket API </p><p>Dynamic CDS Archive </p><p>Uncommit Unused Memory</p></body></html>“””;

Reference: https://openjdk.java.net/jeps/355

Feature 2: Switch Expressions

Java 13, extends Switch Expressions introduced in Java 12 (as preview) .This feature is still in preview. In java 13 ,the keyword ‘break’ is replaced with the keyword ‘yield’’.The yield statement is very similar to return statement.

// Switch Expression using yieldprivate int getHolidayDetails(String day) {return switch (day){case “Saturday”: yield 0;case “Sunday”: yield 0;default: yield 1;};}

Reference: https://openjdk.java.net/jeps/325

Feature 3: Uncommit Unused Memory

The objective of the Garbage Collector is to free heap memory by destroying unreachable objects. There are many garbage collectors present in java and the developers are free to pick the best one for their application’s needs. One such garbage collector is ZGC, which was launched with JDK11.

With Java 13, ZGC has been enhanced to return unused memory to the operating system.

The agenda of ZGC is to keep pause time to below 10ms. When the response time goes beyond 10 ms, the application response time is impacted.

Command to enable ZGC in your application :

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC

Reference: https://openjdk.java.net/jeps/351

Feature 4: Dynamic CDS Archive

This feature evolved over several java releases(10,12 and 13). The goal of Class Data Sharing(CDS) is to reduce the startup time of a java application. It is storing information about classes in Class Data Sharing Archive once and this information can be used at run-time by Java Virtual Machines(JVM).

JVM performs multiple steps on starting to run

  • Loading the byte code
  • Verifying the byte code
  • Pulling the classes into an internal data structure

After completion of the above step, the jars can be used. The drawback here is if the developer is executing the same jar again without any changes then also it is required to go through the same steps again. This is time taking.

Java 13 has come up with a new solution to this problem. When loading the jar, JVM will store the information by executing the above steps. And now it will monitor for changes in the classes. If there is no change in class data, JVM will reuse the information without executing the above steps, for it and only the data for modified classes will be reloaded.

The idea behind AppCDS is to create a class data archive once and then reuse it so that the JVM need not recreate it.

Reference: https://openjdk.java.net/jeps/350

Feature 5: Reimplement the legacy Socket API

A socket is one endpoint that establishes a two-way communication link between two applications. The implementation of the Java Socket API started from JDK 1.0. It is mainly based on the java.net.Socket and java.net.ServerSocket classes. The implementation is a mix of java and C and is very difficult to maintain and debug.

Now JDK 13 comes with a new implementation called NioSocketImpl which no longer requires native code. If you want to stick to the old implementation you can pass the flag -Djdk.net.usePlainSocketImpl.

The new implementation is meant to be easy to adapt to work with-mode threads, also known as fibers, which are being explored in project Loom.

The new changes brings better maintenance and uses java.util.concurrent locks instead of synchronised methods.

Reference: https://openjdk.java.net/jeps/353

--

--