The JEPs in Java 21 deliver several important production-ready advances to the platform.

Java 21 is officially here, as of its general availability launch on September 19, 2023. This release represents the 12th feature release of the platform using the now-standard semiannual cadence. (You should expect to see Java 22 in March 2024.)

There’s a tremendous wealth of information in the Java 21 release notes, and this article will hit some of the highlights. You may also want to review Mohamed Taman’s “Java 21 sneak peek” and Nicolai Parlog’s “Going inside Java 21’s rich, upcoming goodness.”

As described in the official release blog post, Java 21 delivers dozens of new features and enhancements. Fifteen of those appeared as JEPs, which included eight ready-to-use production features. The others are preview and incubator features, which are meant for you and your organization to explore, but which aren’t ready for production use yet, because those features may change in future Java releases.

Much of the new functionality in Java 21 applies to four major platform initiatives: Project Amber (improving developer productivity), Project Loom (reimagining threading), Project Panama (bridging Java and other platforms), and Project Valhalla (augmenting the Java object model with value objects).

Other JEPs enhance Java’s core libraries and garbage collectors, while a few prepare outdated Java features for removal in future platform releases.

Production-ready JEPs

The following are the ready-for-production JEPs included in the Java 21 release.

JEP 431: Sequenced collections. This JEP introduces new interfaces to represent collections with a defined encounter order. Each such collection has a well-defined first element, second element, and so forth, up to the last element. It also provides uniform APIs for accessing a collection’s first and last elements, and for processing its elements in reverse order.

JEP 439: Generational ZGC. This JEP improves application performance by extending the Z Garbage Collector (ZGC) to maintain separate generations for young and old objects. Applications running with Generational ZGC should enjoy lower risk of allocation stalls, lower required heap memory overhead, and lower garbage collection CPU overhead.

JEP 440: Record patterns. This JEP enhances the Java programming language by extending pattern matching to instances of record classes, enabling more-sophisticated data queries. It also adds nested patterns, which can help you create more-composable data queries.

JEP 441: Pattern matching for switch. This JEP improves the productivity of the Java programming language by making it more semantic, so complex data-oriented queries can be expressed concisely and safely. It does so by allowing patterns to appear in case labels, while also requiring that pattern switch statements cover all possible input values.

JEP 444: Virtual threads. Virtual threads can dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. This JEP enables server applications written in the simple thread-per-request style to scale with near-optimal hardware utilization, and it also lets existing code that uses the java.lang.Thread API adopt virtual threads with minimal change.

JEP 449: Deprecate the Windows 32-bit x86 port for removal. The 32-bit version of Windows on x86 processors is obsolescent. It’s supported in Java 21, but it’s going away in the future. This JEP updates the build system to issue an error message when an attempt is made to configure a build for Windows 32-bit x86 (x86-32). The error message will be suppressible via a new configuration option, but the goal is to urge you to move beyond that platform.

JEP 451: Prepare to disallow the dynamic loading of agents. This JEP prepares you for a future release of the JDK that will, by default, disallow the loading of agents into a running JVM. It does so by issuing warnings when agents are loaded dynamically into a running JVM. Note that serviceability tools that load agents at startup will not cause warnings to be issued in any release. This JEP aligns the ability to load agents dynamically with other so-called “superpower” capabilities, such as deep reflection.

JEP 452: Key encapsulation mechanism API. Key encapsulation mechanisms (KEMs) provide modern cryptographic techniques that secure symmetric keys using asymmetric or public key cryptography. This API lets you use several KEM algorithms, and it also allows security providers to implement KEM algorithms in either Java code or native code.

Preview and incubator JEPs

The following are the previews and incubators in Java 21. To use these JEPs, you’ll need to use the appropriate flags; refer to each JEP’s documentation for details.

JEP 430: String templates (preview). This JEP simplifies the writing of Java programs by making it easy to express strings that include values computed at runtime and by enhancing the readability of expressions that mix text and expressions, whether the text fits on a single source line or spans several source lines. It also improves the security of Java programs that compose strings from user-provided values and pass them to other systems.

JEP 442: Foreign function and memory API (third preview). This JEP provides a new API that helps Java programs interoperate with code and data outside of the Java runtime. By efficiently invoking foreign functions (code outside the JVM) and by safely accessing foreign memory (memory not managed by the JVM), the API enables Java programs to call native libraries and process native data without the brittleness and danger of Java Native Interface (JNI).

JEP 443: Unnamed patterns and variables (preview). This JEP enhances the Java language with unnamed patterns, which match a record component without stating the component’s name or type, and unnamed variables, which can be initialized but not used. Both are denoted by an underscore character, _.

JEP 445: Unnamed classes and instance main methods (preview). This JEP offers a smooth on-ramp to Java so educators can introduce programming concepts in a gradual manner. It does so by reducing the boilerplate and ceremony so that students can write their first programs without needing to understand language features designed for large programs; at the same time, it doesn’t introduce a separate beginner’s dialect for Java or a separate toolchain.

JEP 446: Scoped values (preview). This JEP provides a programming model to share data both within a thread and with child threads, to simplify reasoning about data flow. The model ensures that data shared by a caller can be retrieved only by legitimate callees, and it also treats shared data as immutable to allow sharing by many threads and to enable runtime optimizations.

JEP 448: Vector API (sixth incubator). This new API expresses vector computations that reliably compile, at runtime, to optimal vector instructions on supported CPU architectures, thus achieving performance superior to equivalent scalar computations. It also adds the exclusive or (XOR) operation to vector masks, and it improves the performance of vector shuffles, especially when they are used to rearrange the elements of a vector or convert between vectors.

JEP 453: Structured concurrency (preview). This JEP simplifies concurrent programming by introducing an API for structured concurrency, and it promotes a style of concurrent programming that can eliminate common risks arising from cancellation and shutdown, such as thread leaks and cancellation delays.

Download Java 21 and start working today

The Java 21 OpenJDK reference implementation can be acquired from https://jdk.java.net/21/. Oracle’s implementation, Java SE 21, can be found at https://www.oracle.com/java/technologies/downloads/.

Oracle’s Java SE 21 is a long-term support (LTS) release. Oracle will offer long term support for Java 21 for at least eight years. This extended support period gives organizations flexibility to keep applications in production longer with minimal maintenance, and to eventually migrate on their own terms.

Please note that, based on customer feedback and use in the Java ecosystem, Oracle has also announced that long term support for Java 11 has been extended through at least January 2032, providing at least eight more years of support and updates from Oracle.

Dig deeper