Overview of New Features in Java 12

Introduction Java 12 was released on 19th March 2019, six months after the Java 11Click to read: Java 11 long-term support release. It was a short-term release, supported only until Java 13 came out, but it started several features which became important in later versions - most visibly switch expressions, the first preview language feature. This article gives an overview of the new features in Java 12. We will go through its JDK Enhancement Proposals (JEPs), grouped into language, garbage collection and JVM features, and then the new library methods, with links to detailed tutorials. Preview features Java 12 introduced a new way of shipping language changes: as preview features. A preview feature is fully implemented and specified, but not yet permanent, so that developers can try it and give feedback before it is finalized - and it may change, or even be removed, in the next release. Preview features are disabled by default, and must be enabled when both compiling and running -
javac --release 12 --enable-preview Main.java
java --enable-preview Main
Most of the big language features of Java 12 to 25 went through one or more preview rounds before becoming standard. Language features
  1. Switch Expressions (Preview) - JEP 325 - switch can be used as an expression which returns a value, with new arrow (case X ->) labels which do not fall through, and several constants per case. It was previewed again in Java 13 and became a standard feature in Java 14. Read the tutorial on switch expressionsClick to read tutorial: Java Switch Expressions: Arrow Labels and yield.
  2. JVM Constants API - JEP 334 - a new package, java.lang.constant, with types such as ClassDesc and MethodTypeDesc which describe class-file constants without loading them. Classes such as String, Integer and Class got a describeConstable() method. It is mainly for tools and libraries which generate bytecode, and was later used by the Class-File APIClick to read: Class-File API.
Note - Raw string literals (JEP 326) were planned for Java 12, but were withdrawn before the release. The idea came back, redesigned, as text blocksClick to read tutorial: Java Text Blocks: Multi-line Strings with Examples in Java 13 to 15.
Garbage collection and JVM features
  1. Shenandoah: A Low-Pause-Time Garbage Collector (Experimental) - JEP 189 - a garbage collector developed by Red Hat which moves objects while the application is running, so that pause times stay short regardless of the heap size. It became production-ready in Java 15 - read about ZGC and ShenandoahClick to read tutorial: ZGC and Shenandoah: Low-Pause GCs in Java 15.
  2. Default CDS Archives - JEP 341 - the JDK now ships with a class-data sharing archive of its own classes, which is used automatically, so every Java application starts faster with no configuration. See the AppCDS tutorialClick to read: AppCDS tutorial for how class-data sharing works.
  3. Abortable Mixed Collections for G1 - JEP 344 - when G1 sees that a collection will take longer than its pause-time goal, it can stop part-way through, so it meets its pause target more reliably.
  4. Promptly Return Unused Committed Memory from G1 - JEP 346 - when an application is idle, G1 now gives unused heap memory back to the operating system, which matters in containers and the cloud, where you pay for the memory you hold.
  5. Microbenchmark Suite - JEP 230 - a suite of microbenchmarks based on JMH added to the JDK's source code, so that JDK developers can measure the performance effects of their changes.
  6. One AArch64 Port, Not Two - JEP 340 - the JDK had two ports for 64-bit ARM processors; one was removed so that work could focus on the other.
Library additions Java 12 added several useful methods which do not have JEPs of their own. They are explained with examples in the tutorial on the Java 12 API additionsClick to read tutorial: Java 12 API Additions: String.indent, teeing, mismatch -
  • String.indent(n) and String.transform(function),
  • Collectors.teeing(), which combines two collectors over one stream,
  • Files.mismatch(), which finds the first differing byte of two files,
  • NumberFormat.getCompactNumberInstance(), which formats numbers in a short form such as "1K" or "2 million", and
  • exceptionallyAsync() and exceptionallyCompose() on CompletionStage, for handling failures in asynchronous code.
Java 12 also upgraded its Unicode support to Unicode 11.0, adding new characters and emoji. Summary In this article we went through the new features of Java 12. We saw how preview features let developers try language changes before they are finalized, starting with switch expressions, and looked at the JVM Constants API. On the JVM side, Java 12 added the experimental Shenandoah garbage collector, default CDS archives for faster startup, and two G1 improvements. Lastly, we listed the new library methods, such as String.indent(), Collectors.teeing() and Files.mismatch(). Continue with the Java 12 API additions tutorialClick to read tutorial: Java 12 API Additions: String.indent, teeing, mismatch, or move on to the new features in Java 13Click to read tutorial: Overview of New Features in Java 13.