Java 12 New Features

Java SE 12 was released in March 2019.

The full list of new features in JDK 12 can be found here:

https://openjdk.org/projects/jdk/12/ 

In this tutorial, we will look at some of these new features and examine some of them.

1. JEP 189:  Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)

Shenandoah is a new garbage collection (GC) algorithm which reduces GC pause times, independent of heap size. This GC algorithm is experimental so in order to use it with Java 12, you have to enable it :

-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC

2. JEP 325:  Switch Expressions (Preview)

This JEP extends the switch statement to be used as either a statement or an expression. Meaning that we can now return a value from a switch expression.

Before Java 12, we can return a value by assigning it to a variable:

String getColor(int num) {
    String result = "";
    switch (number) {
        case 1, 2, 3:
            result = "red, green or blue";
            break;
        case 4:
            result = "yellow";
            break;
        default:
            result = "black";
            break;
    };
    return result;
}

With Java 12, we can now return a value from a switch via using break or case L ->.

String getColor(int num) {
    String result = 
        switch (number) {
            case 1, 2, 3:
                break "red, green or blue";                
            case 4:
                break "yellow";
            default:
                break "black";
        };
    return result;
}
String getColor(int num) {
    String result = 
        switch (number) {
            case 1, 2, 3 -> break "red, green or blue";                
            case 4 -> "yellow";
            default -> "black";
        };
    return result;
}

3. JEP 341:  Default CDS Archives

Class Data-Sharing feature (CDS) reduces startup time benefiting from memory sharing. However, before Java 12, we have to use -Xshare: dump and generate the CDS archive file for the JDK classes. With Java 12, a new classes.jsa file as a default CDS archive file for the JDK classes is placed under the /bin/server/ directory. 

So good news is that, Java 12 boots faster than the older JDKs. 

4. JEP 344:  Abortable Mixed Collections for G1

5. JEP 346:  Promptly Return Unused Committed Memory from G1


Yorumlar

Popular

Java 14 New Features

Pretenders, Contenders and Liars