Estimate project
← All posts

JDK 22 at a glance

A brief overview of the improvements and new features that Java 22 brings.

6 min read
jvmstructured concurrencyjavajava22jdk22string templatesvector api

Thanks to Java’s steady release schedule, developers look forward to every new version and the improvements and features that come with it. With the next release, Java 22, due on 19th March, it’s a good moment to look at the upgrades and changes ahead. From faster performance to simpler APIs, let’s see what Java 22 offers developers.

Stable Features

Here we cover the features that are now a complete part of Java 22 and improve the platform without needing any special switch to turn them on.

JEP 423: Region Pinning for G1

This is an improvement to the G1 garbage collector whose main goal is to permit garbage collection while JNI regions are active. It tackles G1’s inability to pin arbitrary regions during minor collections – something major collections could already do. With this feature in place, G1 can handle JNI critical regions efficiently without switching GC off: regions holding critical objects get pinned, while garbage collection continues in the unpinned ones. As a result, thread stalls are effectively avoided, along with the latency they could cause.

JEP 454: Foreign Function & Memory API

This improvement brings an API that lets Java work efficiently with foreign functions and memory – that is, code and data living outside the Java Virtual Machine (JVM). The API is meant to supersede the current Java Native Interface (JNI) with a more concise and safer mechanism. Its main features are calling foreign functions and accessing foreign memory with performance on par with or better than JNI, wide platform support, and an emphasis on safety to avoid typical bugs such as use-after-free errors.

JEP 456: Unnamed Variables & Patterns

This enhancement adds unnamed variables and patterns to Java, marked with the underscore _, which simplifies situations involving unused variable declarations or nested patterns. It makes the language leaner and brings it in line with what is common in other programming languages. The change improves developer experience and code readability, letting Java deal with redundant naming in a more intuitive and concise way.

JEP 458 Launch Multi-File Source-Code Programs

This builds on “JEP 330: Launch Single-File Source-Code Programs,” which removed the need for an explicit compilation step in single-file programs. That change made development smoother, which proved especially useful while prototyping. The capability now extends to Java source code programs made up of multiple files, making it even easier to grow from small programs into larger, more complex ones.

Second Preview

This part revisits features from previous Java versions that have now entered their second preview phase in order to collect more feedback. Java preview features are disabled by default; to turn them on, you have to pass the --enable-preview flag explicitly when launching your Java application.

JEP 464: Scoped Values and JEP 462: Structured Concurrency

Neither of these enhancements changed, as stated in their specifications:

Our proposal is to preview the API again in JDK 22, unchanged, so as to collect more experience and feedback.

JEP 459: String Templates

This feature doesn’t bring any notable changes either. Its specification states:

Our proposal is a second preview, to collect more experience and feedback. Apart from a technical change to the types of template expressions, nothing differs from the first preview.

JEP 463: Implicitly Declared Classes and Instance Main Methods

This feature makes declaring a main method simpler, which can be especially helpful for developers new to Java. The first take on this problem arrived in Java 21 with JEP 445: Unnamed Classes and Instance Main Methods, which made it possible to declare a main method with no enclosing class. Following the feedback it received, further refinements were made:

  • A source file lacking an enclosing class declaration is considered to implicitly declare a class whose name is picked by the host system. These implicitly declared classes act like ordinary top-level classes and need no extra tooling, library, or runtime support.
  • The rules for choosing the main method changed: if a candidate main method with a String[] parameter exists, that method is invoked; otherwise a candidate main method without parameters is invoked.

Preview features

This section looks at features that debut as preview features – experimental additions meant for users to try out and give feedback on.

JEP 461: Stream Gatherers

This Stream API enhancement extends it with custom intermediate operations, allowing more flexible and expressive stream pipelines. It handles both finite and infinite streams well. The new Stream::gather(Gatherer) operation makes it possible to build efficient streams that can run in parallel and support many kinds of intermediate operations. The design covers various transformation patterns for stream elements – one-to-one, one-to-many, many-to-one and many-to-many – and ships with built-in gatherers for typical tasks. Developers can also implement the Gatherer interface to fit their particular needs, and several built-in gatherers cover common use cases such as window functions.

This JEP makes Java streams more customizable while adding very little complexity to the Stream API.

JEP 457: Class-File API

Introduced as a preview feature, the Class-File API in Java is intended to standardize the parsing, generation and transformation of Java class files. It tackles a major problem: the JDK’s dependence on the ASM library for manipulating class files. Today the JDK has its own class-file library for the javac compiler, while bundling ASM for other tools and for lambda expression support. This dependence, however, delays the adoption of new class-file features, because ASM updates trail behind JDK releases. The lag impacts tools such as javac, which creates difficulties for developers who want to use new features in major JDK releases.

JEP 447: Statements before super(…)

This feature lets certain statements run before an explicit super(…) constructor call inside class constructors. Its aim is to give developers more freedom in expressing constructor behavior, especially for validating, preparing and sharing arguments. Constructors are still guaranteed to run top-down during class instantiation, without touching uninitialized fields. The language change is backward compatible and requires no modifications to existing code. It marks a notable departure from long-standing Java restrictions, enabling a more natural way of writing constructors without adding complexity.

Incubator

Here we look at features still in an experimental stage that may change in response to user feedback.

JEP 460: Vector API (Seventh Incubator)

JDK 22 brings the seventh incubator of the Vector API. The API lets vector computations be compiled at runtime into optimal vector instructions on supported CPU architectures, outperforming equivalent non-vectorized computations. This round contains small API improvements and bug fixes. Notably, vector access now works with heap MemorySegments backed by an array of any primitive element type, not only byte arrays. The JEP emphasizes a clear, concise and platform-agnostic API design, dependable runtime performance on x64 and AArch64 architectures, and graceful degradation where full vector instruction sets are unavailable. It is aligned with Project Valhalla and its upcoming improvements to the Java object model.

Summary

Although Java 22 is not an LTS release, it delivers 12 interesting improvements. It deals with legacy code problems: Region Pinning for G1 and the Foreign Function & Memory API are meant to reduce the pain associated with JNI. The Class-File API arrives as a step toward removing the dependency on ASM, which will speed up the development of Java tools. The release also contains the 7th incubator of the Vector API, moving the Valhalla project forward. On top of that, it improves developer experience with features such as Unnamed Variables & Patterns, Stream Gatherers and Statements before super, demonstrating Java’s dedication to developing both its foundations and its usability.

Let us know in the comments which feature you consider the most valuable. For me, Stream Gatherers are the most exciting addition.