New Features in Java 13

Rajesh kumar
Sep 14 · 6 min read

In Few days Java 13 will be coming out for General availability on 17th Sep 2019, With Early Access of jdk 13 available now lets start with release schedule and then go through what are the new Features & Enhancements that jdk 13 brings in.

Release Schedule:

2019/06/13 -Rampdown Phase One (fork from main line)
2019/07/18 -Rampdown Phase Two
2019/08/08 -Initial Release Candidate
2019/08/22 -Final Release Candidate
2019/09/17 -General Availability

New Features in Java 13:

1) Text Blocks (Preview) -JEP 355
2) Switch Expressions (Preview) -JEP 354
3) Re-implement the Legacy Socket API -JEP 353
4) ZGC Uncommit Unused Memory -JEP 351
5) Dynamic CDS Archives -JEP 350

Note: To use the preview features available in the jdk use --enable-preview option to enable

1)Text Blocks (Preview) :

With jdk 13 we see a new feature called “text blocks” added to the Java language. A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over format when desired. The main motive behind the text block was today when a developer wanted to have string literal value containing a snippet of HTML/XML/JSON etc.. today it requires significant editing with escapes and concatenation in order to get the desired format and compile the program but with text blocks in place this becomes quite easy.

The normal String is enclosed with pair of double quotes like “..” , the text block is enclosed with pair of Triple Quotes “””..””” and a newline, this is similar to python multi-line string. Below is an example which shows the HTML snippet with normal String and text block.

Normal String:

Normal String

Text Block:

In the example above if you see the Normal string contains double quotes in the content then it needs to be escaped with \” but with text block you specify the double quotes without escaping and even for the new line limiters (\n).you can perform multiple operations on the text block like contacting the normal string with text block which will finally become a text block and resolving the variable within the resulting string.

int i=123;
String testConcatenationStr =”Normal String”+i+”””Text block string”””;

Apart from the concatenation there some other additional methods that are supported for text block

.stripIndent() ->used to strip away incidental white space from the text block
.translateEscapes() -> used to translate escape sequences
.formatted(Object… args) -> simplify value substitution in the text block

So how does the java compiler process text block ? The java compiler process the text block in 3 steps:

1) Line terminators in the content are translated to LF (\u000A). The purpose of this translation is to follow the principle of least surprise when moving Java source code across platforms.

2) Incidental white space surrounding the content, introduced to match the indentation of Java source code, is removed.

3) Escape sequences in the content are interpreted. Performing interpretation as the final step means developers can write escape sequences such as \n without them being modified or deleted by earlier steps.

2) Switch Expression (Preview):

Switch Expression is supported from Java 12 as part of JEP 325 as preview feature ,with Java 13 the switch expression was enhanced to support a new statement called “yield”.A yield statement is used evaluate or return a value for the block. Most of switch expressions will have a single expression to the right of the “case L ->” switch label. But there are cases where you may need to perform multiple operation for a particular switch case where we will go with block {..} and finally need to return a value from the block in this scenario we will use the new statement yield. Let take a business use case for shopping discount ,where we need provide discount to all the customers based on the day in a week they shop for ,and in addition to it based on the customer membership we may need to provide some additional discount on particular day then below will be the code snippet which will make use of yield statement.

In above example yield was used for the switch case “Monday and Friday” where the block {..} for the case is returning a value with yield statement .when yield is used in switch expression there is no need of specifying break statement the break with value statement is dropped in favor of a yield statement, below is an example where you will be not using break statement.

3) Re-implement the Legacy Socket API:

The underlying implementation used by java.net.Socket and java.net.ServerSocket APi’s are pretty much old implementation which date back to jdk 1.0 where the implementation is a mix of legacy Java and C code which has several concurrency issues and further more the future maintenance of this code for fix was difficult so with jdk 13 for this api’s a new simpler and more modern implementation was provided that is easy to maintain and debug. The new implementation comes with the sun.nio.ch.NioSocketImpl where the old implementation comes with java.net.PlainSocketImpl . With jdk 13 java.net.Socket and java.net.ServerSocket API’s uses by default new implementation but the old implementation is still exist with jdk 13 where the users are allowed to switch back to old implementation by setting the system property jdk.net.usePlainSocketImpl.

4) ZGC Uncommit Unused Memory :

ZGC Grabage Collector was firstly introduced with jdk 11 JEP 333 as Experimental feature . With the jdk 13 there was an enhancement made to ZGC where the uncommitted or unused memory was given to operating system back, before this uncommitted or unused memory was held with ZGC which was not an optimal solution for a GC to held even when its not used. With this change it will be helpful in the environments where the memory was a paid resource and also in Environments where an application might be idle for long periods of time and is sharing or competing for resources with many other applications . Another advantage comes where an application might have very different heap space requirements during its execution. For example, the heap needed during start up might be greater than what is needed later during steady state execution.

Other garbage collectors in HotSpot, such as G1 and Shenandoah which comes with jdk12 , provide this capability already today.

5) Dynamic CDS Archives:

In the previous version of jdk 12 there was support of Default CDS Archive with the Jdk 13 we see now Dynamic support where Class Data Sharing(CDS) allows 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 which will Improve the usability of application class-data sharing (AppCDS). Eliminate the need for users to do trial runs to create a class list for each application. providing the class list an archive can be generated by the option -XX:ArchiveClassesAtExit=<filename> The run the same program with shared archive by specifying -XX:SharedArchiveFile=<filename> enables the sharing of class data on top of default archive in the JDK.

JDK 13 builds for different OS can be download from jdk.java.net

To Explore previous version of java , Java 12 Features

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade