Java Development Kit (JDK) 21 Features

  • September 6, 2023

Java Development Kit (JDK) 21 is heading towards its final release. After four previous incubator versions, the eagerly awaited JDK 21 will have 15 finalized features including virtual threads, a generational Z garbage collector, and a key encapsulation mechanism API.

JDK 21 has started its release candidate phase. Oracle encourages developers to try out early-access binaries under the GPL at jdk.java.net.

Let’s review the list of proposed features that made it to the Release Candidate:

  • Structured concurrency is a preview that simplifies concurrent programming via an API that handles groups of related tasks running in different threads as a single unit. The goal is to reduce errors, increase reliability, and enhance observability.
  • Scoped values are another preview and allow the sharing of immutable data within and between threads. This is preferred to thread-local variables and reduces the ceremony associated with writing simple Java programs like scripts and command-line utilities.
  • With this release, an API for key encapsulation mechanisms is added. This encryption technique secures symmetric keys with public cryptography, and can be used in higher-level protocols, such as Transport Layer Security and Hybrid Public Key Encryption.
  • Deprecation of the Windows 32-bit x86 port is one of the key changes. This port is soon to be removed due to the Windows 10 end of life (October 2025). If a build is attempted for this port, an error message will be issued.
  • Unnamed classes and instance main methods are previewed to evolve the Java language and make it easier for students to write their first programs without having to understand complex language features.
  • Unnamed patterns and variables can be initialized but not used. An underscore character is used both as a record component name and as a variables name.
  • Generational ZGC enhances application performance by separating young and old objects in separate generations. This is beneficial for managing memory and reducing CPU overhead as well as avoiding allocation stalls.
  • Record patterns deconstruct record values and enable nested patterns for more composable data queries.
  • Pattern matching for switch allows for more expressive and safety-enabled switch expressions and statements.
  • The vector API will indicate vector computations that can be compiled into optimal instructions for supported CPU architectures.
  • The foreign function and memory API make it easier to access memory outside the Java runtime and call native libraries.
  • Virtual threads are lightweight threads that reduce the effort of writing and maintaining high-throughput concurrent applications. They also provide support for thread-local variables.
  • Sequenced collections integrate with existing collection types for uniform set of operations when a sequence of elements with defined encounter order is needed.
  • Finally, string templates combine literal text with embedded expressions and processors to produce specialized results.

Apart from the JDK Enhancement Proposals, JDK 21 is making a change to the way the JDK assigns names to network interfaces on Windows. Existing code that relies on the NetworkInterface.GetbyName(String name) method must be updated to take this into account. Additionally, JDK Flight Recorder includes several changes to make analysis of flight recordings from the command line easier.

As a long-term support release, JDK 21 will get five additional years of premier support and extended support until 2031. The current LTS release is JDK 17, published in 2021 while the non-LTS versions (JDK 20 and JDK 19) will only receive six months of premier support. LTS versions are released every two years.