Java news: Java JDK 21 – a brief overview of upcoming news

The release of the new version of the Java JDK is approaching and we at our blog decided to take a closer look at the new functionality of the language. The target release date is scheduled for September 19, 2023. However, it is already possible to download this version in Early Access and start the first experiments. (In an earlier article, we discussed the characteristics of the Java 20 JDK.)

Java 21 (after Java 17) is again a long-term support (LTS) release, which means Oracle will provide free updates for at least five years, until September 2028, and extended paid support until September 2031.

Java JDK 21 features
Java JDK 21 features


Let’s briefly introduce the content of JDK 21 and what we can look forward to. We’ll start with the most interesting new features and improvements.

Virtual threads

Virtual threads are lightweight threads that greatly simplify writing, maintaining, and monitoring high-performance concurrent applications. These include the ability to scale server applications written in thread-on-demand form by making efficient use of hardware, the ability to easily migrate existing code that uses the lang.Thread API to virtual threads with minimal changes, and the ability to easily debug and profile virtual threads using existing JDK tools.

Virtual fibers were introduced in JDK 20 and JDK 19 and will be completed in JDK 21. With JDK 21, virtual threads now support local thread variables throughout runtime and are not allowed to create virtual threads without these variables. The guaranteed support for local thread variables will thus ensure that many existing libraries can be used with virtual threads without modification.

Sequenced collections

Sequenced collections, or collections with ordering is another interesting new feature and JDK 21 introduces the design of collections with ordering and introduces interfaces for collections with defined ordering. Each collection has a clearly defined first, second and so on, down to the last element. Uniform APIs are provided for receiving first and last elements and for processing elements in reverse order. The motivation for this proposal is the fact that Java’s collection framework does not have a collection type that represents a sequence of elements with a defined ordering. It also lacks a uniform set of operations that apply to these collections. These gaps have been a problem and a source of complaints. The proposal calls for defining interfaces for sequences of collections, sets and maps and including them in the existing hierarchy of collection types. All these new methods have predefined implementations.

Record patterns

Record patterns enable the deconstruction of record values. Sample records and sample types can be nested, enabling powerful, declarative, and combinable data retrieval and processing. Design goals include extending pattern matching to destruct instances of record classes and adding nested patterns to enable data query composition. This feature was developed in parallel with pattern matching for switch expressions.

Pattern matching for switch

This feature allows a switch expression/command to test itself against multiple patterns, each with a specific action, allowing complex data queries to be expressed safely and concisely. This feature was originally proposed in JDK 17, modified in subsequent versions, and should be completed and modified in JDK 21 based on feedback and experience from Java developers. The main changes from previous versions of JEP include removing parentheses in patterns and allowing qualified enumeration constants, then increasing the safety of expressions and switch statements by requiring that all possible input values are covered. Another goal is to ensure that existing switch writes remain functional without changes and execute with the same semantics.

String templates

This is a feature, in the form of a sample in JDK 21, that complements existing string literals and text blocks in Java by combining such text with embedded expressions and processors to produce specialized results. This language feature and API is intended to simplify the writing of Java programs by making it easy to express strings that contain values computed at runtime. It promises to improve the readability of expressions, increase program security, preserve flexibility, and simplify the use of APIs that accept strings written in languages other than Java. The function also allows the development of non-string expressions created by combining text literals and embedded expressions.

Unnamed patterns and variables

This feature improves code readability and maintenance by allowing patterns to be created without specifying a component name or type. Unnamed variables can be initialized but are not used. The goal is to eliminate unnecessary nested patterns and identify unused variables.

Unnamed classes and instance main methods

This feature is focused on simplifying the writing of first Java programs, where students may not immediately understand language features designed for more complex projects. It allows you to create simple programs and gradually extend them with more advanced functions.

Structured concurrency

Structured concurrency simplifies programming in a concurrent environment by using an API that manages groups of related tasks as units of work. This improves error management, which increases reliability and observability. This feature, which was previously tested in JDK 20 and JDK 19, is now getting support as a sample API in the package util.concurrent.

Scoped values

The preview version of this feature, allows sharing immutable data between threads and components. Scoped values are preferred over thread local variables, especially when using a large number of virtual threads. This feature improves the readability and robustness of the code.

Prepare to disallow the dynamic loading of agents

Disable dynamic agent loading – this change is aimed at improving the integrity of the JVM by alerting on loading agents dynamically while the application is running. The goal is to improve the balance between service changes and code integrity and to ensure that most tools are not affected by the change.

Key encapsulation mechanism API

This feature enables the use of algorithms for securing symmetric keys using public cryptography. The goal is to enable applications to use algorithms such as the RSA Key Encapsulation Mechanism (RSA-KEM) and the Elliptic Curve Integrated Encryption Scheme (ECIES) in a variety of protocols and cryptographic schemes.

Generational ZGC

This feature is aimed at improving application performance by using separate generations for young and old objects. Generational ZGC enables more efficient collection of young objects and results in lower memory consumption and a lower garbage collection burden (collecting unnecessary allocated memory).

Foreign function & memory API

Enables interoperability of Java programs with code and data outside the Java-runtime. It allows calls to external libraries and secure memory access, replacing JNI (Java Native Interface).

Vector API

This API allows calculations to be performed vectorially, resulting in better performance over scalar calculations. The goal is to provide a simple and reliable solution for vector computation on supported architectures.

Deprecate the Windows 32-bit x86 Port for Removal

This change is aimed at removing the 32-bit port for Windows x86 in a future release. Windows 10, the last Microsoft operating system to support 32-bit mode, ends its lifecycle in October 2025.

In addition to these features, JDK 21 is also in the process of changing the naming of network interfaces in Windows. There is also an experimental “compact object headers” feature planned to reduce the size of object headers in the JVM and thus reduce the size of memory.

If you are interested in the new features of Java JDK 21 and would like to learn more about them, we recommend visiting the official OpenJDK website.

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you