Contents

JDK 22 in a nutshell webp image

With the regular cadence of Java releases, developers eagerly anticipate each new version for the enhancements and features it brings. As the next iteration, Java 22, prepares for release on 19th March, it's time to dive into the improvements and updates that await. From performance enhancements to streamlined APIs, let's explore what Java 22 has in store for developers.

Stable Features

This section focuses on the features that are now fully integrated into Java 22, enhancing the platform without requiring any special activation.

JEP 423: Region Pinning for G1

Is an enhancement to the G1 garbage collector, primarily aiming to allow garbage collection during active JNI regions. It addresses the issue of G1's inability to pin arbitrary regions during minor collections, a capability already present in major collections. By implementing this feature, G1 can effectively manage JNI critical regions without disabling GC, pinning regions containing critical objects while allowing garbage collection in unpinned areas. This enhancement will effectively prevent thread stalling and eliminate potential latency related to it.

JEP 454: Foreign Function & Memory API

Improvement introduces an API for Java to efficiently interact with foreign functions and memory, i.e., code and data outside the Java Virtual Machine (JVM). This API aims to replace the existing Java Native Interface (JNI) with a more concise and safer approach. Key features include the ability to invoke foreign functions and access foreign memory with performance comparable to JNI or better, broad platform support, and a focus on safety to prevent common bugs like use-after-free errors.

JEP 456: Unnamed Variables & Patterns

Enhancement Java with unnamed variables and patterns, indicated by the underscore _, optimizes cases where unused variable declarations or nested patterns are involved. This streamlines the language, aligning it with common practices in other programming languages. This refinement boosts developer experience and code clarity, making Java more intuitive and concise in handling redundant naming.

JEP 458 Launch Multi-File Source-Code Programs

This represents a further development from "JEP 330: Launch Single-File Source-Code Programs," which eliminated the explicit compilation step for single-file programs. This advancement facilitated a more fluid development process, particularly valuable during the prototyping phase. Now, this capability has been expanded to multi-file Java source code programs, further easing the transition from small-scale to larger, more complex programs.

Second Preview

In this update, we revisit features from earlier Java versions now in their second preview phase, aimed at gathering further feedback. Preview features in Java are not activated by default. To enable these features, you must explicitly include the --enable-preview flag when running your Java application.

JEP 464: Scoped Values and JEP 462: Structured Concurrency

There were no updates in these enhancements, as their specifications say:

We here propose to re-preview the API in JDK 22, without change, in order to gain additional experience and feedback.

For more information about these improvements, you can check our blog article.

Structured Concurrency

JEP 459: String Templates

In this feature, we also won't observe any significant change. As its specs say:

We here propose a second preview in order 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.

If you are curious about this feature, check our YouTube video where we compare the performance of string concatenation with string templates against traditional methods.

JEP 463: Implicitly Declared Classes and Instance Main Methods

This feature introduces the possibility of easier main method declaration, which can be particularly useful for new Java developers. The initial solution to this challenge was introduced in Java 21 through JEP 445: Unnamed Classes and Instance Main Methods, which provided the possibility of declaring a main method without an enclosing class. However, based on the feedback received, subsequent improvements have been implemented:

  • A source file without an enclosing class declaration is said to implicitly declare a class with a name chosen by the host system. Such implicitly declared classes behave like normal top-level classes and require no additional tooling, library, or runtime support.
  • Rules for selecting the main method were changed: If there is a candidate main method with a String[] parameter then we invoke that method; otherwise we invoke a candidate main method with no parameters.

Preview features

In this section, we'll explore features initially introduced as preview features. These are experimental offerings designed for user testing and feedback.

JEP 461: Stream Gatherers

The enhancement of the Stream API, augments its functionality with custom intermediate operations, enabling more flexible and expressive stream pipelines. This enhancement is adept at managing both finite and infinite streams. The introduction of the Stream::gather(Gatherer) operation allows for the creation of efficient, parallel-capable streams, accommodating a variety of intermediate operations. The design supports multiple transformation patterns of stream elements, including one-to-one, one-to-many, many-to-one, and many-to-many, and includes built-in gatherers for common tasks. Developers can also implement the Gatherer interface to meet specific requirements, and there are several built-in gatherers available for common use cases, such as window functions.

This JEP increases the customizability of Java streams, adding minimal complexity to the Stream API.

JEP 457: Class-File API

The introduction of the Class-File API in Java, as a preview feature, aims to standardize parsing, generating, and transforming Java class files. This addresses the significant issue of JDK's reliance on the ASM library for class-file manipulation. The JDK currently uses its own class-file library for the javac compiler and bundles ASM for other tools and lambda expression support. However, this dependence results in delays in adopting new class-file features, as ASM's updates lag behind JDK releases. This delay affects tools like javac, posing challenges for developers eager to use new features in significant JDK releases.

JEP 447: Statements before super(...)

Introduces a feature that allows certain statements to be executed before an explicit super(...) constructor invocation in class constructors. This update aims to provide developers more flexibility in expressing constructor behaviors, particularly for argument validation, preparation, and sharing. The feature ensures constructors still run top-down during class instantiation without accessing uninitialized fields. This language enhancement is backward compatible and does not require changes in existing code. It represents a significant shift from traditional Java constraints, offering a more natural approach to constructor coding without increasing complexity.

Incubator

In this section we explore features that are in an experimental phase and may evolve based on user feedback.

JEP 460: Vector API (Seventh Incubator)

Introduces the seventh incubator of the Vector API in JDK 22. This API enables vector computations to compile at runtime to optimal vector instructions on supported CPU architectures, enhancing performance over scalar computations. The update includes minor API enhancements and bug fixes. Notably, it supports vector access with heap MemorySegments backed by any primitive element type array, extending beyond just byte arrays. This JEP focuses on clear, concise, and platform-agnostic API design, reliable runtime performance on x64 and AArch64 architectures, and graceful degradation where full vector instruction sets aren't supported. It aligns with Project Valhalla for future Java object model enhancements.

Summary

Java 22, though not an LTS release, introduces 12 interesting improvements. It addresses legacy code issues, with Region Pinning for G1 and Foreign Function & Memory API aiming to minimize pains related to JNI. The Class-File API is introduced as a step toward eliminating dependency on ASM which will accelerate the process of Java tools development. The update includes the 7th incubator of the Vector API, advancing the Valhalla project. Additionally, it enhances developer experience with new features like Unnamed Variables & Patterns, Stream Gatherers, and Statements before super, showcasing Java's commitment to evolving both its foundation and usability.

We'd love to hear from you in the comments about which feature you find most valuable. Personally, I find Stream Gatherers to be the most exciting addition.

Reviewed by: Rafał Maciak, Sebastian Rabiej

Blog Comments powered by Disqus.