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 to switch 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 as case constants in switch 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

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
    See JEP 442: Foreign Function & Memory API (Third Preview) and Foreign Function and Memory API in Java Platform, Standard Edition Core Libraries.
  • 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.