Java 12 : what’s new

Java 12 : what’s new

Now that Java 12 is features complete (Rampdown Phase 2 at the day of writing), it’s time to walk throught all it’s fonctionalities that brings to us, developers, this new version.

This article is part of a serie on what’s new on the last versions of Java, for those who wants to read the others, here are the links : Java 11, Java 10, and Java 9.

Force is to note that besides the switch expression, there is not a lot of new things for developers, we almost had Raw String (multi-line strings) but the functionality was removed from the release at the last moment, I hope it will reappear soon …

JEP 325: Switch Expressions

This is the main new functionality of Java 12, switch expressions allows to define switch as expression in order to be able to get, on a variable, the result of the switch. The switch evolved from a simple control structure (like a set of if/else) to an expression able to calculate a result.

A new syntax has been created, more practicle and concise, that uses the arrow operator already used in lambdas : ‘->’. We can use this new syntax in a classical switch or in a switch expression.

This is an example of the new syntax that uses the arrow operator, you can notice that the break is no longuer needed, this new shape of switch have an implicit break :

switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
    case TUESDAY                -> System.out.println(7);
    case THURSDAY, SATURDAY     -> System.out.println(8);
    case WEDNESDAY              -> System.out.println(9);
}

This other example of a switch expression calculate an integer representing the number of the day, you can notice the ‘;’ at the end of the expression that indicates that we are no longuer in a control structure but in an expression :

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
};

In this example, each case is on a single line, we have an implicit break with the variable (here an integer litteral).

It is possible to write the cases on multiple lines, in this case we must write the break at the end of the code block with the value that the expression must return :

int j = switch (day) {
    case MONDAY  -> 0;
    case TUESDAY -> 1;
    default      -> {
        int k = day.toString().length();
        int result = f(k);
        break result;
    }
};

For now, this new switch is in preview mode, to use it you must add the option –enable-preview to the Java command line, I hope that it will be in final on the next version of Java …

More info on the JEP : https://openjdk.java.net/jeps/325

Various additions :

– https://bugs.openjdk.java.net/browse/JDK-8200435 String::align and String::indent : allow to align or indent a multi-line string.
– https://bugs.openjdk.java.net/browse/JDK-8203442 String::transform : create a new String by transforming the first one with a lambda.
https://bugs.openjdk.java.net/browse/JDK-8202285Files::isSameFile : allow to compare two files to know if they are the same (same content).
– https://bugs.openjdk.java.net/browse/JDK-8205461 : Collectors::teeing : create a collector that is the composition of the two others.
– https://bugs.openjdk.java.net/browse/JDK-8177552java.text.CompactNumberFormat allow to format numbers in a compact shape as defined in the norm  LDML : 1000 -> 1K, 1000000 -> 1M, …

JEP 341: Default CDS Archives

CDS : Class Data Sharing is a functionality of the JVM that allows to reduce the starting time of the JVM, by recording in a file the classes metadata (there representation in the JVM), in order to re-use them in the next launch of the JVM. With the JEP 341, the classes of the JDK will be pre-processed with the CDS mechanism when the JVM will be created, and loaded by default when the JVM will be started (no need to specify any arguments on the command line). So we will benefit for free of the CDS mechanism for the classes of the JDK.

More info in the JEP: https://openjdk.java.net/jeps/341

JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector

The new Garbage Collector (GC): Shenandoah. Developed by Redhat and already included since multiple months in there JVM, it is now integrated as an experimental features in Java 12.

Like ZGC, this is a GC that works concurrentlty to the application, and promise minimal pauses (in the ordrer of milliseconds) for heap of very large size (multiple hundreds of Gb). It targets heaps with large size and machines with multiple cores.

To activate it :

-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC.

More info in the JEP: https://openjdk.java.net/jeps/189

JEP 346: Promptly Return Unused Committed Memory from G1

This functionality allow the G1 Garbage Collector to give back to the OS some memory when it no longuer needs it. For this it add a periodic GC cycle, and, based on the load average of the computer to know it’s usage, decide to return or not some memory to the OS. This functionality is disabled by default and can be enables with :

-XX:G1PeriodicGCInterval=5000 -XX:G1PeriodicGCSystemLoadThreshold=1

More info in the JEP: https://openjdk.java.net/jeps/346

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.