What's New In Java 13

Java 13 is about to be released on 17th Sept 2019. The second release candidate will be available on 22nd August 2019. This post highlights the new features introduced in Java as part of this release. The same features will also be introduced in OpenJDK 13. You may try your hands with the Java 13 by downloading the early access builds released by Oracle at jdk.java.net.

The Build 33 was released at the time of writing this tutorial, hence it covers the important features introduced in Java 13 till Build 33. The features for Java 13 were frozen on 13th June 2019.

New Features in Java 13

This section provides the list of new features introduced in Java 13. The same list is also available on the official website.

JEP 355 - Text Blocks

It's a preview language feature in JDK 13. With the introduction of Text Block i.e. multi-line string literal, the strings can span several lines of code, while avoiding escape sequences in common cases. It also enhances the string readability in Java programs.

// Text Block

String lines = """

This is part 1

This is part 2

""";

JEP 354 - Switch Expressions

The Switch can be used as both statement or expression. Also both the forms can use either traditional form i.e. case ... : labels (with fall through) or case ... -> labels (with no fall through). It's also marked as a preview language feature in JDK 13.

The traditional switch will continue working as it is.

switch( option ) {



case 1:



System.out.println( "Option 1" );



break;

case 2:



System.out.println( "Option 2" );



break;

default:



System.out.println( "Default" );

}

The new switch will work as shown below.

// Statement

switch( option ) {



case 1, 2:



System.out.println( "Option 1 or 2" );



break;

case 3:



System.out.println( "Option 3" );



break;

default:



System.out.println( "Default" );

}



// Expression

String optionVal = switch( option ) {

case 1:

yield "Option 1";

case 2:

yield "Option 2";

default:

throw new IllegalArgumentException( "Invalid option: " + option );

};

// OR

String optionVal = switch( option ) {

case "1" -> "Option 1";

case "2" -> "Option 2";

default -> {

throw new IllegalArgumentException( "Invalid option: " + option );

}

};

JEP 353 - Reimplement the Legacy Socket API

Introduced modern implementation of java.net.Socket and java.net.ServerSocket. The new implementation will be enabled by default. Though the legacy implementation can be enabled by using the system property jdk.net.usePlainSocketImpl.

The new implementation is done considering the compatibility with the old implementation, though the code might result in different behavior with the new implementation. You can also refer to JEP 353 for further details.

JEP 351 - Dynamic CDS(Class-Data Sharing) Archiving

Application class-data sharing(AppCDS) has been simplified in terms of usage. The classes can be dynamically archived while the application is exiting. The dynamically-generated archive will be created on top of the default system archive packaged with the running JDK image.

We can use the option -XX:ArchiveClassesAtExit=<file name> to generate an archive when the program exits. We can run the same application with the option -XX :SharedArchiveFile = <file name> to use the dynamic archive.

# Create the Archive

% bin/java -XX:ArchiveClassesAtExit=hello.jsa -cp hello.jar Hello



# Use the Archive

% bin/java -XX:SharedArchiveFile=hello.jsa -cp hello.jar Hello



# Use the Dynamic Archive with Base Archive

% bin/java -XX:SharedArchiveFile=<base archive>:hello.jsa -cp hello.jar Hello

JEP 350 - Uncommit Unused Memory

In the previous releases, ZGC as not returning the unused heap memory to the operating system. It's enhanced to return unused heap memory to the operating system. This feature is enabled by default, but it can be explicitly disabled for an application using the option -XX:- ZUncommit .

An uncommit delay can be configured using the option -XX:ZUncommitDelay=<seconds> (defaults to 300 seconds). This delay specifies for how long memory should have been unused before it's eligible for uncommit.