On September 17 2019, Java 13 was released GA (General Availability).
Java 13 / JDK 13: General Availability: https://t.co/06XlNb5Nty #jdk13 #java13 #openjdk #java
— Mark Reinhold (@mreinhold) September 17, 2019
This blog post gives an overview of what’s new in Java 13, and helps you decide if you should upgrade. Please dive in, reading time is short, I promise!
New language features
Text Blocks (preview)
Java 13 introduces the concept of Text Blocks. Remember messy String concatenation like this?
String html = "<html>\n" + " <body>\n" + " <p>Hello, Craftsmen!</p>\n" + " </body>\n" + "</html>\n";
Text Blocks take away some of the previous pain:
String html = """ <html> <body> <p>Hello, Craftsmen</p> </body> </html> """;
Some properties of a Text Block:
- The Text Block starts after the opening delimiter of three quotes, followed by spaces and a line terminator.
- The text Block ends at encountering three quotes. So, in the example above, the closing </html> element is still followed by a newline.
- Indentation is determined by removing all indentation that is shared by all lines. So, in the example above, the <html> tag will be at “column 1” in the string.
- You can still use escape characters like \r, \n etc.
Switch expressions (preview)
In Java 13, Switch Expressions continue where Java 12 left off. A switch expression is an expression in the form of a switch statement that yields a value:
int j = switch (day) { case MONDAY -> 0; case TUESDAY -> 1; default -> { int k = day.toString().length(); int result = f(k); yield result; } };
Interesting observations:
- Switch expressions are exhaustive. This means that every possible value of the input must be covered. If not, you will get a compilation error.
- In Java 12, a multi-statement expression branch would end in “break <some result>”. The keyword break has been replaced with yield for switch expressions in Java 13, to clearly separate switch statements from switch expressions. It is not allowed to use break in switch expressions and vice versa. That’s a good thing, since break means breaking out of a control flow, and yield actually delivers a value.
So, how do I use these new features?
As said, these are preview features for evaluation by users. The semantics and syntax of these features might change in a next version of Java. To enable them, use the –enable-preview parameter:
javac --enable-preview (classpath) (java source) java --enable-preview (classpath) (java jar or class file)
Some interesting API additions
String::formatted(Object… objects)
This instance method has the same behaviour as String.format(String s, Object… objects), but on the string instance itself.
Some Text Block behaviour is now also exposed by the API:
String::stripIndent
Removes all shared indentation from a String, exactly how it’s done in text blocks.
String::translateEscapes
Translates escape sequences, like it’s done in text blocks.
There’s a new Socket implementation
The old, badly maintainable SocketImpl has been reimplemented by NioSocketImpl that does not depend on any native code. If you use the Socket and ServerSocket classes, you will automatically get the new implementation. If, for whatever reason, you still want the old implementation, you can instruct the JVM to do so:
java -Djdk.net.usePlainSocketImpl (classpath) (jar or class files)
Now, this is also a preparation to work with Fibers from Project Loom. Check out the Wiki for more information on Fibers.
Z Garbage collector enhancements
The Z Garbage Collector, or ZGC, focuses on keeping garbage pause times low and independent of heap size. Now, ZGC will return unused memory to the operating system by default, whereas in Java 12 it did not.
(re)starting quicker with AppCDS
Application Class Data Sharing (AppCDS) basically creates an immutable archive of classes that is loaded quickly into memory when the JVM starts, greatly reducing startup time.
- Up until Java 12, you had to create this archive manually.
- In Java 12, a default archive was created with JDK classes that are used frequently to help with starting the JVM faster.
In Java 13, you can instruct the JVM to create an archive of loaded Class data for your specific application into a file on normal JVM exit:
java -XX:ArchiveClassesAtExit (classpath) (jar or class file)
A custom CDS archive will then be created for your application needs, greatly speeding up startup on subsequent runs.
Should you upgrade?
In our opinion, if you’re already on Java 11, Java 13 offers some nice production-ready features, alongside an improved ZGC and the possibility to start up faster. So if you’re into optimizing memory usage or startup time, you might give it a shot.
As far as language features go, they’re still experimental, so upgrading for that gives no production value right now.
Java 12 or 13 are not LTS (Long-Term Support) releases, so this means that:
- If you’re on a Java 12 Oracle OpenJDK, support ended when Java 13 came out, so you have to upgrade.
- Alternatively, you can grab a JDK version from AdoptOpenJDK or other providers, which have longer support for previous versions.
IDE support
IntelliJ 2019.2 and Eclipse 2019-09 already support Java 13.
Summary
Java 13 brings some neat experimental features to the language, as well as improvements in garbage collection performance and startup time.
All in all, the current 6-month release cycle of Java significantly improves the possibilities to experiment with new features and brings small improvements faster.
You can expect a blog about Java 14 next spring!
If you want to play around with the new language and API features, you can find a demo project here: