Java 13 Updates revealed Checkout here

 

Java 13 doesn’t have too many major updates which are usually called a JEP (Java Enhancement Proposal). Here is a list of JEPs which were delivered in the new version of Java:

  • Switch Expressions (Preview)
  • Text Blocks (Preview)
  • Reimplement the Legacy Socket API
  • Dynamic CDS Archives
  • ZGC: Uncommit Unused Memory

Most likely, the first two updates are going to be the most interesting for most of Java developers since they introduce new syntax in the Java language.

Switch Expressions (Preview)

Switch expressions have been already introduced in Java 12. Making a long story short, JEP 325 added a new simplified form of a switch which looks like the following:

T result = switch (arg) {
case L1 -> e1;
case L2 -> e2;
default -> e3;
};

If you are not aware of the new syntax, here is an example:

int result = switch (s) {
case "Foo" -> 1;
case "Bar" -> 2;
default -> throw new IllegalArgumentException("Oops!");
};

Java 13 adds a new yield a keyword which allows returning a value from a switch block:

int result = switch (s) {
case "Foo" -> 1;
case "Bar" -> 2;
default -> {
System.out.println("Neither Foo nor Bar, hmmm...");
yield 0;
}
};

Here we can see that a switch an expression can be used to calculate a value for a local variable without assigning the value in each and every case block. The yield the statement is used in the default block to return a value but it can be used in a regular case block.

Now the bad news. You might have noticed the “preview” word in the JEP’s title. It means that the new syntax is not enabled by default in Java 13. If you would like to play with the new switch expressions, you have to run the Java compiler with --enable-preview --release 13 options, and then start your program with --enable-preview option:

$ javac -d classes --enable-preview --release 13 Test.java
$ java -classpath classes --enable-preview Test

Text Blocks (Preview)

Java 13 has another language update. It is text blocks which help to define multi-line strings. For example, an HTML snippet or an SQL query are often defined as a multi-line string in Java. Here is what it usually looks like:

String html = "<html>\n" +
" <body>\n" +
" <p>Hello, world</p>\n" +
" </body>\n" +
"</html>\n";

You might have noticed a lot of \n, + and " characters which make the code a bit longer and harder to read. Some programming languages such Python allow defining multi-line strings by using triple quotes """ to start and end them. Such a language feature may help to make code much nicer. Finally, Java 13 introduces such a feature. Here is how the example above can be re-written using the new text blocks:

String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";

At runtime, the string above becomes a usual String (note that the indentation spaces are removed at runtime):

<html>
<body>
<p>Hello, world</p>
</body>
</html>

Although the new text blocks may look simple, there are quite a lot of important topics around it such as line terminators, indentation, incidental white space, escape sequences, concatenation. You can find all the details in JEP 355.

Unfortunately, the new text block is also available only in preview mode.

The rest

JEP 353 replaces the underlying implementation of the java.net.Socket and java.net.ServerSocket APIs. The main motivation for this update wat that the old implementation has several problems and doesn’t fit well to the plans of further Java development.

The Java sockets are the main component for networking. If the update introduces a regression, it’s going to affect many applications. Fortunately, the old implementation was not removed. If something goes wrong, jdk.net.usePlainSocketImpl system property can be used to switch back to the old implementations. However, the old implementation and the system property are going to be removed in one of the next JDK releases.

Next, JEP 351 introduces a new option-XX:ZUncommitDelay for the Z Garbage Collector (ZGC). The option tells ZGC when it should uncommit and return memory to the operating system.

Finally, JEP 350 is an update for application class-data sharing (AppCDS). The JEP the application class-data sharing to allow the dynamic archiving of classes at the end of Java application execution.

Conclusion

Java 13 doesn’t have too many JEPs. Besides major updates, the new Java release has around 2500 bug fixes and minor enhancements. Since the new text blocks and switch expressions are still previewed language features, most likely we won’t see a big difference after migrating to the new Java version.

 

Related blog:

Spring boot tutorial

Digital marketing training in  chennai