Back
October 9, 2020

What’s new in Java 13

Share:

n this article, we will cover changes that came with Java version 13.

Dynamic CDS ArchivesThis feature enhances the Application Class Data sharing which was introduced in Java 10. The idea behind this feature is to improve startup performance by creating class-data archive that JVM can reuse.
Read more at https://blog.codefx.org/java/application-class-data-sharing/

ZGC: Uncommit Unused MemoryThe JEP 333: Z Garbage Collector is introduced in Java 11, it provides a short pause times when cleaning up heap memory. However, it didn’t return unused heap memory to the operating system, even when that memory has been unused for a long time.

This JEP enhanced the ZGC by returning unused heap memory to the operating system.

Reimplement the Legacy Socket APIThe old default implementation of Socket API (PlainSocketImpl) dates back to java 1.0 and is really hard to debug and maintain. In Java 13 it is replaced with NioSocketImpl. If for some reason we need still to use PlainSocketImpl we can do it by passing -Djdk.net.usePlainSocketImpl to JVM.

Switch Expressions (Preview)Switch expressions were introduced in Java 12 as a preview feature.
(Preview feature is a new concept introduced in Java 12. A preview language or VM feature is a new feature of the Java SE Platform that is fully specified, fully implemented, and yet impermanent.)
The impermanent nature of preview features can be seen through changes that came with Java 13. With Java 12 we could use break to return value from a switch expression. Now, break is no longer compiled and we should use yield.
Java 12:

String numberOfDays = switch (month) {
case 2:
break "28";
case 4, 6, 9, 11:
break "30";
default:
break "31";
};

Java 13:

String numberOfDays = switch (month) {
case 2:
yield "28";
case 4, 6, 9, 11:
yield "30";
default:
yield "31";
};

Note: switch expressions is still a preview feature and they can change more in the following versions.

Text Blocks (Preview)

This feature gives us a possibility to instantiate String as a text block.
Text blocks are surrounded with ””” (triple quotation marks).

The old way of writing text blocks:

String jsonExample ="{\n" +
\"region\":\"lika\"
"}\n";

The new way of writing text blocks:

String jsonExample = """
{
"region":"lika"
}
""";


Share: