Java 13

Java 13 was released on 17 September 2019. Let’s see what new features it provided.

Syntax features

##JEP 355: Text Blocks (Preview) The preview feature has multi-line Strings. In the past storing JSON as String required a lot of escaping like:

String JSON = "{\"version\": 13,\"language\": \"java\"}";

With this feature, we can use multi-line Strings without needing to escape double quotes or to add a carriage return:

String JSON2 = """
{
  "version": 13,
  "language": "java"
}
""";

JEP 354: Switch Expressions (Second Preview)

The switch feature was introduced as preview in Java 12, via JEP 325: Switch Expressions (Preview). Feedback was sought initially on the design of the feature, and later on the experience of using switch expressions and the enhanced switch statement.

Switch was extended by yield statement. The yield statement exits the switch and returns the result of the current branch, similar to a return.

int cost = switch (day) {
  case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY  -> {
    yield 1;
  }
  case SATURDAY, SUNDAY -> {
    yield 2;
  }
};

java.lang.String

String has new methods:

  • stripIndent() – mimics the compiler to remove incidental white space
  • translateEscapes() – translates escape sequences such as “\t” to “\t”
  • formatted() – works the same as String::format, but for text blocks

JEP 350: Dynamic CDS Archives

Extend application class-data sharing to allow the dynamic archiving of classes at the end of Java application execution. The archived classes will include all loaded application classes and library classes that are not present in the default, base-layer CDS archive.

To use it, you need to add JVM option

-XX:ArchiveClassesAtExit=<archive filename>

To include archive file during the start, use:

-XX:SharedArchiveFile

JEP 351: ZGC: Uncommit Unused Memory (Experimental)

Enhance ZGC to return unused heap memory to the operating system.

JEP 353: Reimplement the Legacy Socket API

Replace the underlying implementation used by the java.net.Socket and java.net.ServerSocket APIs with a simpler and more modern implementation which is easy to maintain and debug.

Summary

Java 13 was, for me, just another iteration of improvements without huge changes in stable features.