Ukraine needs your help!

Support
Series: What is new in Java?

What is new in Java 13?

Java

September 17, 2019 was released a new version of Java. According to tradition of this series, I described the whole list of features for Java 13.

This post was published later, because of building the series “What is new in Java?” of releases of Java starting from Java 11.

JEP 350: Dynamic CDS Archives

JEP 350 simplifies the shared use of data classes of AppCDS applications.

AppCDS was added to Java 8 and till to Java 13 release was actively enhanced. Finally, Java 13 includes this feature to provide the dynamic archivation of classes at the end of Java application execution.

AppCDS - Application Class Data Sharing is a JVM feature for startup acceleration and memory saving.

The idea behind AppCDS is to share once loaded classes between JVM instances on the same host. It seems that it should be fit for microservices, especially for Spring Boot “broilers” that have thousands of library classes.

To be able to use CDS we have to allow access to archives when Java Application will be ending the execution.

java -XX:ArchiveClassesAtExit=myApp.jsa -cp myApp.jar Hello

As a result, we could use the early created dynamic archive, the content that will be allocated at myApp.jsa file.

java -XX:SharedArchiveFile=myApp.jsa -cp myApp.jar Hello

JEP 351: ZGC: Uncommit Unused Memory

Initially, ZGC was introduced in Java 11, but before Java 13 it wasn’t returned not used memory of heap back to the operation system, unlike garbage collector G1. JEP 351 resolved this issue and ZGC was enabled by default.

JEP 353: Reimplement the Legacy Socket API

Java 13 replaced the base implementation which was used by API interfaces of java.net.Socket and java.net.ServerSocket, to more simply and adaptive, which is easy to support and debug.

Socker API has been added to JDK 1.0, the implementation was based on deprecated code of Java and C, which was difficult to support and debug.

JEP 354: Switch Expressions (Preview)

The first time, Switch Expression was introduced in Java 12 as a preview feature. It extends syntaxes of switch operation, which allows using the expressions in the switch.

But in Java 13 with a JEP 354 added the modifications and a new keyword yield to return the value from the case instead of using break.

This means, that the switch expression which is returning a value has to use the yield operator for that, but the regular switch still uses the break operator.

int number = switch (arg) {
    case "1": yield 1;
    case "2": yield 2;

    default: {
        yield 0;
    }
}

JEP 355: Text Blocks (Preview)

Earlier, Java does not have a construction that allows for comfortable storing multiline strings like HTML, JSON, or SQL fragments.

String html = "<body>\n" +
              "    <p>Hello, bcode.dev</p>\n" +
              "</body>\n";

Java 13 introduced as a preview feature the construction to do that when we can use multiline strings like that:

String html = """
              <body>
                  <p>Hello, world</p>
              </body>
              """;

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022