Java 13 : what’s new ?

Java 13 : what’s new ?

Now that Java 13 is features complete (Release Candidate at the day of writing), it’s time to walk throught all it’s functionalities that brings to us, developers, this new version.

This article is part of a series on what’s new on the last versions of Java, for those who wants to read the others, here are the links : Java 12, Java 11Java 10, and Java 9.

Force is to note that, again, there is not a lot of new functionalities in this release. Besides the new Text Block that replaces the Raw Strings, there is not a lot of modifications on the JDK.

JEP-355: Text Blocks (Preview)

This is the main new functionality in the version 13 of Java, the capability to write Text Blocks : a new type of String Literal that allow to write them on multiple lines.

Text Blocks don’t add a lot of new functionalities (they are not raw string, there is no string interpolation, …), they only allow to write strings on multiple lines, and automatically manage the indentation for us. Instead of using a single escape character, they use an escape sequence: """

System.out.println("""
    Hello,
    multiline
    text blocks!""")

Speaking of indentation, this is what is the most specific about this new functionality : a complex algorithm has been implemented to keep the indentation as the developer intended to define it. Concretely, the indentation is done by deleting the identation before the first letter (so here we delete the indentation before the H of Hello), this is called the incidental indentation.

The main rules of Text Blocks are :

  • Start by """ and a new line.
  • Delete the incidental indentation and the first line break.
  • Keep the rest of the indentation.
  • End by """ without line break. If there is one it will be added to the end of the string !
  • If there is a new line at the end of the Text Block, it’s position will define the incidental indentation instead of the Text Block first character.
  • You can use double-quote inside a Text Block.

To implement Text Blocks, new methods has been added to the String class. But as this functionality is a preview feature, you need to activate it via --enable-preview to be able to use them : JDK-8203444 – String::formatted, JDK-8223775 – String::stripIndent et JDK-8223781 – String::translateEscapes.

More info : https://openjdk.java.net/jeps/355 and in this very complete article by Nicolai Parlog : https://blog.codefx.org/java/text-blocks/

You can also read the Programmer’s Guide To Text Blocks by Jim Laskey and Stuart Marks : http://cr.openjdk.java.net/~jlaskey/Strings/TextBlocksGuide_v8.html

JEP 354 – Switch Expressions (Preview)

Unfortunately, Switch Expressions stays in preview! Their implementation has been modified, a new keyword yield has been created to return a value from a switch branch. Before we use break "value"; now we need to use yield "value";.

The idea behind this is to be able to reuse this keyword later (for example for if expression ?).

More info in the JEP : https://openjdk.java.net/jeps/354

Shenandoah:

A lot of news in Shenandoah, the new GC implemented par Red Hat and integrated into OpenJDK.

The more important ones are the support of 32 bits JVM and the elimination of the forwarding pointer word that allow to reduce the memory footprint of Shenandoah with the addons of load reference barriers.

More info in this series of 3 posts on the Red Hat blog : https://developers.redhat.com/blog/?p=602377

Other

Via the JEP 353: Reimplement the Legacy Socket API, the API java.net.Socket and java.net.ServerSocket has been totaly rewritten, their implementations dated back to JDK 1.0, and the work in progress on threads, a.k.a. fibers, for the Project Loom needed some modifications.

There is also some news around security : https://seanjmullan.org/blog/2019/08/05/jdk13

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.