What is new in Java 12

Java 12 logo

Java 12 reached General Availability on 19 March 2019, download Java 12 here or this openJDK archived.

Java 12 features.

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

Shenandoah is a new low-pause and concurrent garbage collector, read this research paper, it reduces GC pause time and independent of the Java heap size (5M or 5G of heap size have the same pause time, useful for large heap applications.)

This GC is an experimental feature, and we need to use the following options to enable the new Shenandoah GC.


-XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC

However, both Oracle JDK and OpenJDK don’t contain this new Shenandoah GC.


C:\Users\mkyong> java -version
java version "12" 2019-03-19
Java(TM) SE Runtime Environment (build 12+33)
Java HotSpot(TM) 64-Bit Server VM (build 12+33, mixed mode, sharing)

C:\Users\mkyong> java -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
Error occurred during initialization of VM
Option -XX:+UseShenandoahGC not supported

The AdoptOpenJDK 12 may contain this Shenandoah GC, but I can’t download the AdoptOpenJDK 12 for testing. Let me know the result if you can test it.

Further Reading
Shenandoah OpenJDK Home

JEP 230: Microbenchmark Suite

Added a range of Java Microbenchmark Harness (JMH) benchmarks to the JDK source code, for those interested to add or modify the JDK source code itself, now they have a way to compare the performance.

Note
Sorry, not sure how it works, please comment below if you know how to run the JDK’s benchmark tests.

JEP 325: Switch Expressions (Preview)

This JEP enhanced the existing switch statements (returns nothing) to support switch expressions (returns something).

Traditional switch statements, we can return a value by assigning the value to a variable:


	private static String getText(int number) {
        String result = "";
        switch (number) {
            case 1, 2:
                result = "one or two";
                break;
            case 3:
                result = "three";
                break;
            case 4, 5, 6:
                result = "four or five or six";
                break;
            default:
                result = "unknown";
                break;
        };
        return result;
    }

In Java 12, we can use break or case L -> to return a value from a switch.


	private static String getText(int number) {
        String result = switch (number) {
            case 1, 2:
                break "one or two";
            case 3:
                break "three";
            case 4, 5, 6:
                break "four or five or six";
            default:
                break "unknown";
        };
        return result;
    }

case L -> syntax.


	private static String getText(int number) {
        return switch (number) {
            case 1, 2 -> "one or two";
            case 3 -> "three";
            case 4, 5, 6 -> "four or five or six";
            default -> "unknown";
        };
    }

Futher Reading
Java 12 Switch expressions

Note
This switch expressions have a second preview in Java 13 (dropped the break in favor of yield), and this switch expressions became a standard feature in Java 14.

To enable the Java 12 preview features:


javac --enable-preview --release 12 Example.java
java --enable-preview Example

JEP 334: JVM Constants API

A new package java.lang.constant, a list of new classes and interfaces to model the key class-file and run-time artifacts, for example, the constant pool.

Further Reading
Package java.lang.constant

JEP 340: One AArch64 Port, Not Two

Before Java 12, there are two different source code or ports for the 64-bit ARM architecture.

  • Oracle – src/hotspot/cpu/arm
  • Red Hat? – src/hotspot/cpu/aarch64

Java 12 removed the Oracle src/hotspot/cpu/arm port, and maintain only one port src/hotspot/cpu/aarch64, and make this aarch64 the default build for the 64-bit ARM architecture.

JEP 341: Default CDS Archives

The Class Data Sharing (CDS) improved the start-up time by reuse an existing archive file.

Before Java 12, we need to use -Xshare: dump to generate the CDS archive file for the JDK classes. In Java 12, there is a new classes.jsa file in the /bin/server/ directory, a default CDS archive file for the JDK classes.

default cds

JEP 344: Abortable Mixed Collections for G1

This JEP improves the performance of the Garbage-first (G1) collector, by splits the problematic collection set into two parts – mandatory and optional. The G1 will abort the optional part if lack of time to handle it.

JEP 346: Promptly Return Unused Committed Memory from G1

This JEP improves the performance of the Garbage-first (G1) collector. If the application is low of the activity or idle, G1 periodically trigger a concurrent cycle to determine overall Java heap usage and return unused Java heap memory to the operating system.

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-12

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar