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 -
Most of the big language features of Java 12 to 25 went through one or more preview rounds before becoming standard.
Language features
Garbage collection and JVM features
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 -
javac --release 12 --enable-preview Main.java java --enable-preview Main
- Switch Expressions (Preview) - JEP 325 -
switchcan 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. - JVM Constants API - JEP 334 - a new package,
java.lang.constant, with types such asClassDescandMethodTypeDescwhich describe class-file constants without loading them. Classes such asString,IntegerandClassgot adescribeConstable()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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
String.indent(n)andString.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", andexceptionallyAsync()andexceptionallyCompose()onCompletionStage, for handling failures in asynchronous code.
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.Continue Reading Java 12 to 17 Tutorials
Java 12 API Additions: String.indent, teeing, mismatchClick to read tutorial: Java 12 API Additions: String.indent, teeing, mismatch Overview of New Features in Java 13Click to read tutorial: Overview of New Features in Java 13 Overview of New Features in Java 14Click to read tutorial: Overview of New Features in Java 14 Overview of New Features in Java 15Click to read tutorial: Overview of New Features in Java 15 Overview of New Features in Java 16Click to read tutorial: Overview of New Features in Java 16 Overview of New Features in Java 17Click to read tutorial: Overview of New Features in Java 17 Java Switch Expressions: Arrow Labels and yieldClick to read tutorial: Java Switch Expressions: Arrow Labels and yield Java 14 Helpful NullPointerExceptions ExplainedClick to read tutorial: Java 14 Helpful NullPointerExceptions Explained Java 14 JFR Event Streaming Tutorial with ExamplesClick to read tutorial: Java 14 JFR Event Streaming Tutorial with Examples
Java 12 API Additions: String.indent, teeing, mismatchClick to read tutorial: Java 12 API Additions: String.indent, teeing, mismatch Overview of New Features in Java 13Click to read tutorial: Overview of New Features in Java 13 Overview of New Features in Java 14Click to read tutorial: Overview of New Features in Java 14 Overview of New Features in Java 15Click to read tutorial: Overview of New Features in Java 15 Overview of New Features in Java 16Click to read tutorial: Overview of New Features in Java 16 Overview of New Features in Java 17Click to read tutorial: Overview of New Features in Java 17 Java Switch Expressions: Arrow Labels and yieldClick to read tutorial: Java Switch Expressions: Arrow Labels and yield Java 14 Helpful NullPointerExceptions ExplainedClick to read tutorial: Java 14 Helpful NullPointerExceptions Explained Java 14 JFR Event Streaming Tutorial with ExamplesClick to read tutorial: Java 14 JFR Event Streaming Tutorial with Examples