JDK 22: The new features in Java 22 coming in March '24
Sheersho P.
SSE Java @ EY | Spring Boot | Microservices | Ex-Sapient | 3k+ Followers & 1M+ Views
Java Development Kit 22, set to arrive in March 2024 as the next version of Java Standard Edition, has grown to include eight features so far, with a preview of a class-file API, region pinning for the G1 garbage collector, and a second preview of implicitly declared classes and instance methods proposed in the past few days.
A possible ninth feature, stream gatherers, has been tentatively designated for JDK 22 but was not listed yet on the official JDK 22 OpenJDK web page as of November 20.
These latest features join a preview of statements before super(…), a second preview of string templates, unnamed variables and patterns, a foreign function and memory API, and a seventh incubator of a vector API. Early-access builds of JDK 22 are available at jdk.java.net for Linux, Windows, and Mac
The class-file API, a preview feature, would provide a standard API for parsing, generating, and transforming Java class files. It is intended to enable JDK components to migrate to the standard API and eventually remove the JDK’s internal copy of the third-party ASM library. Parsing, generating, and transforming class files is ubiquitous, used by independent tools and libraries to examine and extend programs without jeopardizing maintainability of source code.
However, the Java class-file format evolves more quickly now than previously, due to standard Java’s six-month release cadence. This speed of evolution has resulted in frameworks more frequently encountering class files that are newer than the class-file library. This has resulted in errors and in framework developers trying to write code to parse class files from the future and hoping nothing too serious will change. The plan is to have the Java platform define and implement a standard class-file API to evolve together with the class-file format.
Region pinning for G1 is intended to reduce latency, so that garbage collection (GC) need not be disabled during Java Native Interface (JNI) critical regions. Goals include no stalling of threads due to critical JNI regions, no added latency to start garbage collection due to these regions, no regressions in GC pause times when none of these regions are active, and minimal regressions in GC pause times when these regions are active. Currently, the default GC, G1, disables garbage collection during every critical region, which can have a significant impact on latency. With this change, Java threads will never wait for a G1 GC operation to complete.
Implicitly declared classes and instance methods is currently being previewed in JDK 21 as unnamed classes and instance methods. This feature would get the revised title and significant changes in JDK 22 in a second preview. The capability is intended to evolve the Java language so students could write their first programs without needing to understand language features designed for large programs. Students could write streamlined declarations for single-class programs and then seamlessly expand programs to use more advanced features as their skills grow. The second preview makes changes to class declaration and method invocation that simplify the original proposal.
Stream gatherers, a preview in JDK 22, would enhance Java’s Stream API to support custom intermediate operations. This would allow stream pipelines to transform data in ways not easily achievable with existing built-in intermediate operations. Goals include making stream pipelines more flexible and expressive and, insofar as possible, allowing custom intermediate operations to manipulate streams of infinite size. An API would be provided for custom intermediate operations that allows developers to transform finite and infinite streams in preferred ways.
The preview of statements before super(…) pertains to constructors in the language, allowing statements that do not reference the instance being created to appear before an explicit constructor. A goal of the plan includes giving developers greater freedom to express the behavior of constructors, enabling more natural placement of logic that currently must be factored into auxiliary static methods, auxiliary intermediate constructors, or constructor arguments.
Another goal is preserving the existing guarantee that constructors run in top-down order during class instantiation, ensuring that code in a subclass constructor cannot interfere with superclass instantiation. A third stated goal is not requiring any changes to the JVM. This is the only JDK 22 feature, so far, that has not yet been previously previewed or incubated in standard Java.
String templates, in a second preview in JDK 22, would complement Java’s existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results. Goals include:
领英推荐
String templates appear in a first preview in JDK 21. The second preview is intended to gain additional experience and feedback. Except for a technical change in the types of template expressions, there are no changes relative to the first preview.
A vector API (seventh incubator) would express vector computations that reliably compile at runtime to optimal vector instructions on supported CPU architectures, achieving performance superior to equivalent scalar computations. The API provides a way to write complex vector algorithms in Java, using the existing HotSpot auto-vectorization algorithm but with a user model that makes vectorization more predictable and robust.
This capability has been incubated in prior versions of Java dating back to JDK 16 in March 2021. Goals of the API include it being clear and concise, platform-agnostic, and offering reliable runtime compilation and performance on x64 AArch64 architectures as well as graceful degradation. This capability is intended to leverage Project Valhalla enhancements to the Java object model, enabling programs to work with value objects.
Unnamed variables and patterns can be used when variable declarations or nested patterns are required but never used. Goals of the plan include:
This proposal was previewed in JDK 21 and would be finalized without change in JDK 22.
The foreign function and memory API allows Java programs to interoperate with code and data outside of the Java runtime. By invoking foreign functions and safely accessing foreign memory, Java programs can call native libraries and process native data without the brittleness of JNI (Java Native Interface), the proposal states.
The foreign function and memory API previously was previewed in JDK 19, JDK 20, and JDK 21. It would be finalized in JDK 22. The latest revisions cover three areas: supporting arbitrary charsets for native strings, enabling clients to build C-language function descriptors programmatically, and introducing the Enable-Native-Access JAR-file manifest attribute. The latter allows executable JAR files to call restricted methods without having to use the —enable-native-access command-line option.
Due March 19, 2024, Java 22 is a Feature release that will receive six months of support from Oracle, unlike just-released JDK 21, which is a Long Term Support (LTS) release that will receive at least eight years of support. Companies besides Oracle could offer longer support for JDK 22 if they so choose. Standard Java updates occur every six months, and LTS releases arrive every two years.
Other capabilities that could appear in JDK 22 include structured concurrency, which simplifies concurrent programming by treating groups of related tasks running in different threads as a single unit of work, and computed constants, which are immutable value holders initialized at most once.
Prior to general availability, JDK 22 is set to go through rampdown phases in December and January, followed by two release candidates in February 2024.