JDK 22: Exploring the Latest Enhancements in Java 22

Scheduled for release in March 2024, Java Development Kit 22 (JDK 22) promises a slew of exciting features, expanding the capabilities of Java Standard Edition. The upcoming version has accumulated eight notable features, including a preview of the class-file API, region pinning for the G1 garbage collector, and a second preview of implicitly declared classes and instance methods, proposed recently.

As of November 20, an additional potential feature, stream gatherers, has been tentatively outlined for inclusion in JDK 22, although it has not yet been officially listed on the JDK 22 OpenJDK web page.

The class-file API, offered as a preview feature, is poised to introduce a standardized API for parsing, generating, and transforming Java class files. The primary objective is to facilitate the migration of JDK components to this standardized API, ultimately eliminating the need for the JDK’s internal copy of the third-party ASM library. Parsing, generating, and transforming class files are fundamental operations widely employed by independent tools and libraries for program analysis and extension, ensuring the longevity of source code.

However, the rapid evolution of the Java class-file format, driven by the six-month release cadence of standard Java, has presented challenges. Frameworks often encounter class files newer than their libraries, resulting in errors and developers attempting to parse class files from the future with the hope that significant changes will not impede functionality. The plan is to establish a standard class-file API within the Java platform that evolves in tandem with the class-file format.

Addressing latency concerns during Java Native Interface (JNI) critical regions, region pinning for the G1 garbage collector aims to enhance performance. The goal is to eliminate the need to disable garbage collection during JNI critical regions, ensuring that Java threads are not stalled due to these critical regions. The current default GC, G1, introduces latency by disabling garbage collection during each critical region, impacting overall system responsiveness. With this proposed change, Java threads will no longer be impeded by G1 GC operations.

Implicitly declared classes and instance methods, currently in preview in JDK 21 under the name unnamed classes and instance methods, are set for a significant overhaul in JDK 22’s second preview. This feature seeks to evolve the Java language to enable students to write programs without delving into complexities designed for larger-scale projects. The second preview introduces changes to class declaration and method invocation, simplifying the original proposal.

Stream gatherers, proposed as a preview in JDK 22, aim to enhance Java’s Stream API by supporting custom intermediate operations. This would empower stream pipelines to manipulate data in ways not easily achievable with existing built-in intermediate operations. The objective is to make stream pipelines more flexible and expressive, allowing custom operations to handle streams of both finite and infinite size.

The preview of statements before super(…) focuses on constructors, permitting statements unrelated to the instance being created before an explicit constructor. The plan aims to provide developers with greater freedom in expressing constructor behavior, allowing for more natural placement of logic that might otherwise be confined to auxiliary static methods or constructor arguments. Importantly, the plan maintains the guarantee that constructors run in a top-down order during class instantiation, preserving the integrity of superclass instantiation.

String templates, in their second preview in JDK 22, complement Java’s existing string literals and text blocks by combining literal text with embedded expressions and template processors. The goals include simplifying Java program writing, improving the readability of expressions mixing text and computations, enhancing program security by supporting validation, and allowing Java libraries to define formatting syntax used in string templates. The second preview builds on the first, introducing a technical change in the types of template expressions.

The seventh incubator, a vector API, enables the expression of vector computations that compile at runtime to optimal vector instructions on supported CPU architectures. This API, incubated since JDK 16, aims for clear and concise syntax, platform-agnosticism, reliable runtime compilation, and performance on x64 AArch64 architectures. Leveraging Project Valhalla enhancements to the Java object model, the vector API facilitates working with value objects.

Unnamed variables and patterns, previewed in JDK 21, address situations where variable declarations or nested patterns are necessary but unused. This feature captures developer intent, improves code maintainability by identifying unused variables, and allows multiple patterns in a single case label. The proposal remains unchanged and is set for finalization in JDK 22.

The foreign function and memory API, previewed in JDK 19, JDK 20, and JDK 21, facilitates Java program interoperability with code and data outside the Java runtime. The latest revisions cover areas such as supporting arbitrary charsets for native strings, programmatically building C-language function descriptors, and introducing the Enable-Native-Access JAR-file manifest attribute.

JDK 22, set for release on March 19, 2024, is classified as a Feature release, offering six months of support from Oracle. In contrast, JDK 21, a recently released Long Term Support (LTS) release, provides at least eight years of support. Beyond Oracle, other companies may extend support for JDK 22. The standard Java update cycle involves releases every six months, with LTS releases occurring every two years.

Potential additions to JDK 22 include structured concurrency, simplifying concurrent programming, and computed constants, immutable value holders initialized at most once. Before its general availability, JDK 22 will undergo rampdown phases in December and January, followed by two release candidates in February 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *