Overview of New Features in Java 16
Introduction
Java 16 was released on 16th March 2021, with seventeen JDK Enhancement Proposals (JEPs). It was one of the most important releases between the Java 11Click to read: Java 11 and Java 17Click to read tutorial: Overview of New Features in Java 17 long-term support versions: it finalized records and pattern matching for
JVM and garbage collection
Development of the JDK and library additions
The JDK's source code moved from Mercurial to Git (JEP 357) and to GitHub (JEP 369), where it is developed openly as
instanceof, made jpackage a standard tool, and strongly encapsulated the JDK's internals by default. This article gives an overview of the new features in Java 16, with links to detailed tutorials.
Language features
- Records - JEP 395 - a one-line way to declare classes which carry immutable data, with the constructor, accessors,
equals(),hashCode()andtoString()generated. Read the records tutorialClick to read tutorial: Java 16 Records Tutorial with Examples. - Pattern Matching for instanceof - JEP 394 -
if (obj instanceof String s)tests, casts and binds a variable in one step. Read about pattern matching for instanceofClick to read tutorial: Java 16 Pattern Matching for instanceof. - Sealed Classes (Second Preview) - JEP 397 - finalized in Java 17. Read the sealed classes tutorialClick to read tutorial: Java 17 Sealed Classes Tutorial with Examples.
- Packaging Tool - JEP 392 -
jpackagecreates native installers and self-contained application images which include their own Java runtime. Read the jpackage tutorialClick to read tutorial: jpackage Tutorial: Packaging Java Apps (Java 16). - Strongly Encapsulate JDK Internals by Default - JEP 396 - deep reflection into the JDK's internal classes is denied by default. Read about the strong encapsulation of JDK internalsClick to read tutorial: Strong Encapsulation of JDK Internals in Java 16 and 17.
- Warnings for Value-Based Classes - JEP 390 - the constructors of the primitive wrapper classes, such as
new Integer(5), were deprecated for removal, and the compiler and JVM now warn about synchronizing on instances of value-based classes likeIntegerandLocalDate. This prepares for the value classes of Project Valhalla. - Unix-Domain Socket Channels - JEP 380 - socket channels can communicate through Unix-domain sockets, including on Windows. See the Java 16 API additionsClick to read tutorial: Java 16 API Additions: Stream.toList, mapMulti and More.
- Alpine Linux Port - JEP 386 and Windows/AArch64 Port - JEP 388 - the JDK runs on Alpine Linux, popular for small container images, and on Windows on ARM processors.
- ZGC: Concurrent Thread-Stack Processing - JEP 376 - ZGC moved the scanning of thread stacks out of its pauses, so its pauses no longer grow with the number of threads - typically well under a millisecond. Read about ZGC and ShenandoahClick to read tutorial: ZGC and Shenandoah: Low-Pause GCs in Java 15.
- Elastic Metaspace - JEP 387 - the memory used for class metadata is returned to the operating system more promptly, and uses less memory overall.
- Vector API (Incubator) - JEP 338 - an API for SIMD vector computations, which the JIT compiler turns into the vector instructions of modern CPUs. It was still incubating in Java 25.
- Foreign Linker API (Incubator) - JEP 389 and Foreign-Memory Access API (Third Incubator) - JEP 393 - the two halves of what became the Foreign Function & Memory APIClick to read: Foreign Function & Memory API in Java 22.
openjdk/jdk, and its C++ code can now use C++14 features (JEP 347). In the libraries, Java 16 added Stream.toList(), Stream.mapMulti(), day-period formatting and more - read about the Java 16 API additionsClick to read tutorial: Java 16 API Additions: Stream.toList, mapMulti and More.
Summary
In this article we went through the new features of Java 16. Records and pattern matching for instanceof became standard, jpackage arrived for packaging applications, and the JDK's internals were strongly encapsulated by default. We also looked at the warnings for value-based classes, Unix-domain sockets, the new Alpine Linux and Windows/AArch64 ports, ZGC's sub-millisecond pauses, and the incubating APIs which became the Vector and Foreign Function & Memory APIs. Next, read the overview of new features in Java 17Click to read tutorial: Overview of New Features in Java 17.Continue Reading Java 12 to 17 Tutorials
Java 16 Records Tutorial with ExamplesClick to read tutorial: Java 16 Records Tutorial with Examples Java 16 Pattern Matching for instanceofClick to read tutorial: Java 16 Pattern Matching for instanceof Java 16 API Additions: Stream.toList, mapMulti and MoreClick to read tutorial: Java 16 API Additions: Stream.toList, mapMulti and More jpackage Tutorial: Packaging Java Apps (Java 16)Click to read tutorial: jpackage Tutorial: Packaging Java Apps (Java 16) Strong Encapsulation of JDK Internals in Java 16 and 17Click to read tutorial: Strong Encapsulation of JDK Internals in Java 16 and 17 ZGC and Shenandoah: Low-Pause GCs in Java 15Click to read tutorial: ZGC and Shenandoah: Low-Pause GCs in Java 15 Overview of New Features in Java 12Click to read tutorial: Overview of New Features in Java 12 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
Java 16 Records Tutorial with ExamplesClick to read tutorial: Java 16 Records Tutorial with Examples Java 16 Pattern Matching for instanceofClick to read tutorial: Java 16 Pattern Matching for instanceof Java 16 API Additions: Stream.toList, mapMulti and MoreClick to read tutorial: Java 16 API Additions: Stream.toList, mapMulti and More jpackage Tutorial: Packaging Java Apps (Java 16)Click to read tutorial: jpackage Tutorial: Packaging Java Apps (Java 16) Strong Encapsulation of JDK Internals in Java 16 and 17Click to read tutorial: Strong Encapsulation of JDK Internals in Java 16 and 17 ZGC and Shenandoah: Low-Pause GCs in Java 15Click to read tutorial: ZGC and Shenandoah: Low-Pause GCs in Java 15 Overview of New Features in Java 12Click to read tutorial: Overview of New Features in Java 12 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