This Specification defines version 21 of the Java Platform, Standard Edition (“Java SE 21”). The Reference Implementation of this Specification is the Java Development Kit, version 21 (“JDK 21”).
This release continues the evolution of the Platform to ensure the
broadest possible success of the core Java technology.
It standardizes two language features:
record patterns and pattern matching for switch
statements
and switch
expressions.
It previews three new language features:
String templates, unnamed classes and instance main
methods,
and unnamed patterns and variables.
This release standardizes APIs in three areas: virtual threads which are
lightweight threads useful for writing high-throughput concurrent
applications, sequenced collections which are collections with a
well-defined order, and key encapsulation mechanisms which use encryption
to secure symmetric keys using public key cryptography.
It previews a structured concurrency API, simplifying multithreaded
programming, and a scoped value API, enabling sharing of immutable data
within and across threads.
This release also re-previews foreign function and memory APIs.
Finally, this release issues warnings to prepare users for a future
release in which a running JVM will no longer dynamically load agents by
default.
This document directly specifies API features, enhancements, clarifications, and bug fixes. It also specifies features by reference to other Specifications which are revised in Maintenance Releases of existing JSRs. The specifications of these changes are contained in two accompanying documents: Annex 1 is the complete Java SE 21 API Specification and Annex 2 is an annotated API specification showing the exact differences relative to Java SE 20. Informative background for these changes may be found in the list of approved Change Specification Requests for this release.
This Specification includes the Java SE 21 Editions of The Java Language Specification and The Java Virtual Machine Specification in Annex 3. The Java SE 21 Editions contain all corrections and clarifications made since the Java SE 20 Editions, as well as additions for new features. Annex 3 also presents changes to The Java Language Specification and The Java Virtual Machine Specification in connection with preview language features in Java SE 21.
This Specification includes the Java SE 21 versions of additional narrative specifications in Annexes 4 — 11. The Java SE 21 versions contain all corrections and clarifications made since the Java SE 20 versions, as well as additions for new features.
Changes to the Java SE Platform Specification are categorized as either features or enhancements. A feature is, roughly speaking, a change of which at least one of the following statements is true:
Any addition that is not a feature is considered an enhancement.
There is, obviously, room for judgment when interpreting this definition. In order to maximize the visibility of Platform revisions we generally tend to consider borderline items to be features rather than enhancements.
Some Component JSR Specifications previously incorporated into the Platform are still available separately or have significant Specifications themselves. Changes to their Specifications are therefore made in separate Maintenance Releases, which are incorporated here by reference:
JSR 269: Pluggable Annotation-Processing API [MR 15]
Each Component JSR Specification, or revision thereto, may be related to one or more features in the detailed list below.
Work on features in the Java SE 21 Reference Implementation is organized in terms of JDK Enhancement Proposals (JEPs). Each feature description gives a link to the corresponding JEP document as a convenience, but that document is not a normative part of this Specification. Some features are included in the Java SE 21 Reference Implementation on a preview basis, to gain exposure before achieving permanent status in a later release of the Java SE Platform.
Summary
Introduce an API by which Java programs can interoperate with code and data outside of the Java runtime. By efficiently invoking foreign functions (i.e., code outside the JVM), and by safely accessing foreign memory (i.e., 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 JNI. This is a preview API.
Introduce scoped values, which enable the sharing of immutable data within and across threads. They are preferred to thread-local variables, especially when using large numbers of virtual threads. This is a preview API.
Introduce 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 its first and last elements, and for processing its elements in reverse order.
"Life can only be understood backwards; but it must be lived forwards."
— Kierkegaard
Simplify concurrent programming by introducing an API for structured concurrency. Structured concurrency treats groups of related tasks running in different threads as a single unit of work, thereby streamlining error handling and cancellation, improving reliability, and enhancing observability. This is a preview API.
Introduce virtual threads to the Java Platform. Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications.
Introduce an API for key encapsulation mechanisms (KEMs), an encryption technique for securing symmetric keys using public key cryptography.
Enhance the Java programming language with pattern matching for switch
expressions and statements. 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.
Enhance the Java programming language with record patterns to deconstruct record values. Record patterns and type patterns can be nested to enable a powerful, declarative, and composable form of data navigation and processing.
Enhance the Java programming language with string templates. 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. This is a preview language feature and API.
Evolve the Java language so that students can write their first programs without needing to understand language features designed for large programs. Far from using a separate dialect of Java, students can write streamlined declarations for single-class programs and then seamlessly expand their programs to use more advanced features as their skills grow. This is a preview language feature.
Enhance 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, _
. This is a preview language feature.
Issue warnings 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. Serviceability tools that load agents at startup will not cause warnings to be issued in any release.
Preview features A preview feature is a new feature of the Java language, Java Virtual Machine, or Java SE API that is fully specified, fully implemented, and yet impermanent. Preview features must possess the following properties:
Preview features may be removed in a future release, or upgraded to permanent features of the Java SE Platform.
The Java SE API consists solely of Java APIs, but the complete Java SE Platform includes non-Java APIs, such as JNI and JVM TI, and language-independent protocols, such as JDWP and Java Object Serialization. A preview feature may include changes to non-Java APIs and language-independent protocols.
Implementations of this Specification must:
Additional details about preview features, including a description of a preview feature’s life cycle and the relationship of preview features in the Java language to preview features in the Java SE API, are available in JEP 12 Preview Features.
This section may be compared to the corresponding section that appeared in Java SE 18.
Restricted methods Various methods in the Java SE API allow Java code to interoperate with resources outside the Java runtime in such a way that the runtime cannot prove correct or safe use of the resources. These methods, which are provided because of their high utility, are specified as having the potential to crash the Java runtime or corrupt memory. They are known as restricted methods.
All methods in the Java SE API that are not restricted are unrestricted. Given the potential danger of restricted methods, developers should use restricted methods only when no suitable functionality is available in unrestricted methods. To encourage developers to seek unrestricted alternatives to restricted methods, the following rule applies:
java.base
module, or (2) the ModuleLayer.Controller.enableNativeAccess
, or (3) as
provided for An Implementation may provide a means to invoke its run-time system with all restricted methods treated as unrestricted when invoked from code identified to the run-time system.
If the run-time system is invoked in this way, then by definition there are no restricted methods for the identified code to invoke, so no warnings are issued during the execution of such code.
If code other than that identified to the run-time system invokes a restricted method, the Implementation may give a signal other than a warning issued on the standard error stream.
(The Reference Implementation provides the ability to treat
restricted methods as unrestricted for code in certain modules via
the command-line option --enable-native-access=M
.
M
is a comma-separated list of modules whose code is
permitted to invoke restricted methods as if they were unrestricted, i.e.,
without warnings. The special operand
ALL-UNNAMED
indicates every unnamed module, which includes code on the class
path. When the --enable-native-access
option is present, any
invocation of restricted methods from code outside the listed modules will
cause an IllegalCallerException
to be thrown, rather than a
warning to be issued.)
The restricted methods in Java SE 21 are:
Preparing for removal of finalization An Implementation must support the finalization of objects, as described in The Java Language Specification, section 12.6. However, the Java SE 18 Platform Specification deprecates finalization, for removal. To aid preparations for the removal of finalization, an Implementation may provide a means to invoke its run-time system with finalization disabled. If finalization is disabled, the effect is that the Java Virtual Machine never invokes an object’s finalizer before the storage for the object is reclaimed by the garbage collector. An Implementation must not, by default, disable finalization.
(The Reference Implementation provides the ability to disable
finalization via the command-line option
--finalization=disabled
.)
Future revisions of this Platform Specification are expected to disable finalization by default and, eventually, to remove finalization from the Java Language Specification.
The list of Java APIs which are preview features may be found in the Preview API section of the API Specification. They are disabled by default at compile time and run time.
(The Reference Implementation provides the ability to
enable the entire set of preview features via the command-line option
--enable-preview
to javac
and the
java
launcher.)
No non-Java APIs or language-independent protocols were added to the Java SE Platform by this Platform Specification.
Preview features may be removed in a future release, or upgraded to permanent features of the Java SE Platform.
A module is a named set of packages designed for reuse. A specification governed by the JCP defines standard packages, and may group them into one or more standard modules.
This Specification groups the standard packages of the Java SE Platform into 21 standard modules, which we refer to as the Java SE modules. The name of a Java SE module always starts with the string "java.". The complete list of such modules is:
java.base
java.compiler
java.datatransfer
java.desktop
java.instrument
java.logging
java.managementjava.management.rmi
java.naming
java.net.http
java.prefs
java.rmi
java.scripting
java.se (aggregator)
java.security.jgss
java.security.sasl
java.sql
java.sql.rowset
java.transaction.xa
java.xml
java.xml.crypto
Compared to Java SE 20, this Specification does not add or remove any modules.
The module graph The Java SE modules depend upon each other as stated in their specifications, which are part of the overall API Specification. The corresponding complete Java SE module graph has too many edges to be displayed easily in visual form; here is the transitive reduction of the directed acyclic graph, in which redundant edges are omitted (click to enlarge):
Here is how to read this visualization of the module graph:
If one module depends upon another, and it grants implied readability to that module via a requires transitive directive, then there is an edge from the first module to the second.
At the very bottom is the java.base module, which contains essential classes such as java.lang.Object and java.lang.String. The base module depends upon no module, and every other module depends upon the base module.
At the top is the java.se module, which gathers together all of the modules that comprise the Java SE Platform. This is an example of an aggregator module, which logically gathers the content of other modules by granting implied readability to them, but adds no content of its own. A run-time system configured to contain the java.se module will contain all of the packages of the Java SE Platform.
A module is a Java SE module — that is, considered part of the Java SE Platform Specification — if and only if it is a standard module reachable from the java.se module.
Relaxing strong encapsulation As an aid to migration, previous versions of this Specification permitted an Implementation to provide a means to invoke its run-time system with one or more packages of one or more of its modules open to code in all unnamed modules, i.e., to code on the class path. This is no longer permitted.
(The Reference Implementation provided this capability via the
command-line option --illegal-access=permit
.)
An Implementation must not relax the strong encapsulation of any of its modules, either by default or upon request. That is, its run-time system must not behave as if some packages in the Implementation’s modules are open when they are not open according to their module declarations. A package, or an entire module, is open to code in all unnamed modules if and only if:
This section may be compared to the corresponding section that appeared in Java SE 16.
Incorporated subsections This Specification incorporates the following subsections by reference from the Java SE 9 Platform Specification (JSR 379), “Modules”:
java.compiler
. The following APIs were removed from the Java SE Platform by this Platform Specification. The number in parentheses indicates the Java SE Platform Specification which first proposed its removal.
Classes
Methods
The following APIs were proposed for removal from the Java SE Platform by the Platform Specifications for Java SE 9, Java SE 10, Java SE 13, Java SE 14, Java SE 16, Java SE 17, Java SE 18, Java SE 19, and Java SE 20. They are not removed in this release of the Java SE Platform. They continue to be eligible for removal in a future release.
Packages
Classes
Exceptions
Methods
Constructors
Fields
The following APIs are proposed for removal from the Java SE Platform
by this Platform Specification. They are annotated as
@Deprecated
with forRemoval=true
, following the
convention established by
Enhanced Deprecation.
This makes them eligible for removal in a future release.
Methods
Additional details about deprecations, including potential alternatives, may be found in the Deprecated API section of the API Specification. Migration away from deprecated APIs is strongly encouraged.