Java 13 (JDK 13) -The New Features
Java Development Kit (JDK) 13, the latest version of standard Java, is now available and release with some new features of,
1. Switch Expressions
This is one of the preveiew language feature defined in java 13.
Java 13 extends the previous Java 12 Switch Expressions by adding a new yield keyword to return a value from switch expression.So breaks syntax is no longer compiled in Java 13, it uses yield instead.
Check switch expression with java 13.
String number=switch (number) { case 1: yield “one”; case 2: yield “two”; default : yield “Zero”; }
2.Text Blocks
This also preveiew language feature .
A text block is a multi-line string literal that automatically formats the string in a predictable manner and gives developers control over the format.This is a new syntax you get in JDK 13.
This enhances the readability of strings in programs that denote code written in non Java languages and allow a better way of writing, and more importantly reading, multi-line text inside Java code.
As an example ,
//multi-line string literal before Java 13 String html ="<html>\n" + " <body>\n" + " <p>Hello, World</p>\n" + " </body>\n" + "</html>\n"; //new multi-line string literal String html = """ <html> <body> <p>Hello, World</p> </body> </html> """;
3.Dynamic CDS Archives
Application class-data sharing (AppCDS) was available in Java 10 and improved in Java 12 and 13. The idea behind AppCDS is to create a class-data archive once and then share it, so that the JVM not need to recreate it. It reduces launch times and response time by moving much of the class-loading work out of the program run. Instead of loading class data from JARs when it’s needed.
Java 10 this happen through the three steps of creating a list of classes to archive , creating the archive and launching with the archive. Java 12 introduce a default archive of JDK classes that is shipped with the JVM and used automatically.
But in Java 13,
Extending AppCDS to enable dynamic archiving of classes at the end of application execution. Archived classes would include all loaded application and library classes not present in the default, base-layer CDS archive. The new option -XX:ArchiveClassesAtExit tells the JVM to run as usual, but at the end of execution write the class-data into the specified file. This file can be used to run the application with the archive CDS.
//run without CDS & create archive $ java -XX:ArchiveClassesAtExit=hello.jsa -cp hello.jar Hello
//use created an archive (To run the program with the CDS archive above) $ bin/java -XX:SharedArchiveFile=hello.jsa -cp hello.jar Hello
4.ZGC: Uncommit Unused Memory
The Z Garbage Collector (ZGC) is a scalable low-latency garbage collector introduced in Java 11, it provides a short pause times when cleaning up heap memories. With Java 13, ZGC enhance to return unused heap memory to the operating system.
JEP-351: ZGC: Uncommit Unused Memory
5.Reimplement the Legacy Socket API
The legacy APIs introduced with JDK 1.0 .With java 13 this replace the underlying implementation used by the java.net.Socket and java.net.ServerSocket APIs with a simpler and more modern implementation that is easy to maintain and debug. The new implementation will be easy to adapt to work with user-mode threads.
Pingback: JDK 14 - The new features in Java 14 - Sandny Blog()