Significant Changes in JDK 21 Release
See JDK 21 Release Notes for additional descriptions of the new features and enhancements, and API specification in JDK 21.
The following are some of the updates in Java SE 21 and JDK 21:
Language Features
- Record patterns and type patterns can be nested to enable a
powerful, declarative, and composable form of data navigation and processing.
You can use a record pattern to test whether a value is an instance of a record
class type and, if it is, to recursively perform pattern matching on its
component values. In this release, support for record patterns appearing in the
header of an enhanced
for
statement has been removed.See JEP 440: Record Patterns and Record Patterns in Java Platform, Standard Edition Java Language Updates.
- A
switch
statement transfers control to one of several statements or expressions, depending on the value of its selector expression. Extending pattern matching toswitch
allows an expression to be tested against a number of patterns, each with a specific action, so that complex data-oriented queries can be expressed concisely and safely. In this release:- Parenthesized patterns have been removed.
- Qualified
enum
constants ascase
constants inswitch
expressions and statements are allowed.
See JEP 441: Pattern Matching for switch and Pattern Matching for switch Expressions and Statements in Java Platform, Standard Edition Java Language Updates.
Preview Language Features
- String templates complement Java's existing string literals and text blocks by
coupling literal text with embedded expressions and template processors to
produce specialized results.
See JEP 430: String Templates (Preview) and String Templates in Java Platform, Standard Edition Java Language Updates.
- Unnamed patterns match a record component without stating the component's name
or type. Unnamed variables are variables that can be initialized but not used.
You denote both with the underscore character (
_
).See JEP 443: Unnamed Patterns and Variables (Preview) and Unnamed Patterns and Variables in Java Platform, Standard Edition Java Language Updates.
- Unnamed classes and instance main methods enable students to write streamlined
declarations for single-class programs and then seamlessly expand their programs
later to use more advanced features as their skills grow.
See JEP 445: Unnamed Classes and Instance Main Methods (Preview) and Unnamed Classes and Instance Main Methods in Java Platform, Standard Edition Java Language Updates.
See Preview Language and VM Features for more information about preview features.
Library Improvements
- Virtual threads are lightweight threads that reduce the effort of writing,
maintaining, and debugging high-throughput concurrent applications.
See JEP 444: Virtual Threads and Virtual Threads in Java Platform, Standard Edition Core Libraries.
- Sequenced collections are 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 is an interface that represents a
sequenced collection, provides uniform APIs for accessing its first and last
elements, and for processing its elements in reverse order.
See JEP 431: Sequenced Collections and Creating Sequenced Collections, Sets, and Maps in Java Platform, Standard Edition Core Libraries.
- The Key Encapsulation Mechanism API is introduced for key
encapsulation mechanisms (KEMs), an encryption technique for securing symmetric
keys using public key cryptography.
See JEP 452: Key Encapsulation Mechanism API and The KEM Class in Java Platform, Standard Edition Security Developer's Guide.
Library Improvements Previews and Incubator
- The preview API Foreign Function and Memory API has been further
refined as follows:
- Centralized the management of the lifetimes of native
segments in the
Arena
interface - Enhanced layout paths with a new element to dereference address layouts
- Provided a linker option to optimize calls to functions
that are short-lived and will not upcall to Java (e.g.,
clock_gettime
) - Provided a fallback native linker implementation, based on
libffi
, to facilitate porting - Removed the
VaList
class
- Centralized the management of the lifetimes of native
segments in the
- Structured concurrency treats multiple tasks running in different
threads as a single unit of work, thereby streamlining error handling and
cancellation, improving reliability, and enhancing observability.
See JEP 453: Structured Concurrency (Preview) and Structured Concurrency in Java Platform, Standard Edition Core Libraries.
- Scoped values enables sharing of immutable data within and across
threads. They are preferred to thread-local variables, especially when using
large numbers of virtual threads.
See JEP 446: Scoped Values (Preview) and the
ScopedValue
class in the Java API Specification. - The Vector API is introduced to express vector computations that reliably compile at runtime to optimal vector instructions on supported CPU architectures, thus achieving performance superior to equivalent scalar computations. See JEP 448: Vector API (Sixth Incubator).
See JEP 12: Preview Features and JEP 11: Incubator Modules for more information about preview features and incubating APIs.
Performance Improvements
Application performance has been improved by extending the Z Garbage Collector (ZGC) to maintain separate generations for young and old objects. This will allow ZGC to collect young objects, which tend to die young, more frequently.
See JEP 439: Generational ZGC and The Z Garbage Collector in Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide.
Stewardship
Warnings are issued when agents are loaded dynamically into a running JVM. These warnings aim to prepare users for a future release which disallows the dynamic loading of agents by default in order to improve integrity by default.
See JEP 451: Prepare to Disallow the Dynamic Loading of Agents.
Removed APIs, Tools, and Components
For more details on removals and deprecations, see Features and Options Removed and Deprecated in JDK 21.
In addition, there are security related updates that you need to be aware of. See Security Updates in JDK 21.