JDK 21 · JSR 396 · Long-term support

Java 21

JDK 21 is Java SE 21, JSR 396, generally available on 19 September 2023. Vendors treat it as a long-term support release. Virtual threads, pattern matching for switch, record patterns, and sequenced collections are final. String templates, scoped values, structured concurrency, unnamed patterns, and unnamed classes are previews. The existing Java 21 example and the concurrency chapter stay the detailed sources.

How to read the status

Language

String Templates (Preview)

JEP 430 · Preview · Language

String templates are a preview for interpolating values into a string or another result. They are not a permanent feature.

Second preview in Java 22 (JEP 459). They are not in the JDK 23, JDK 24, or JDK 25 feature lists.

OpenJDK JEP 430 (leaves JavaMastery)

Unnamed Patterns and Variables (Preview)

JEP 443 · Preview · Language

An underscore can stand for a variable or a pattern component that the program does not use. This is a preview.

Final in Java 22 (JEP 456).

OpenJDK JEP 443 (leaves JavaMastery)

Unnamed Classes and Instance Main Methods (Preview)

JEP 445 · Preview · Language

A small program can omit the class wrapper and use an instance main method. This is a preview.

The idea continues through later previews and is finalized in Java 25 as compact source files (JEP 512).

OpenJDK JEP 445 (leaves JavaMastery)

APIs / Libraries

Scoped Values (Preview)

JEP 446 · Preview · APIs / Libraries

Scoped values are a preview API for sharing immutable data down a call, including across virtual threads.

Incubated in Java 20 (JEP 429). Final in Java 25 (JEP 506).

OpenJDK JEP 446 (leaves JavaMastery)

Related reading: Concurrency

Foreign Function & Memory API (Third Preview)

JEP 442 · Preview · APIs / Libraries

The foreign function and memory API is previewed a third time. It is not final in Java 21.

Final in Java 22 (JEP 454).

OpenJDK JEP 442 (leaves JavaMastery)

Vector API (Sixth Incubator)

JEP 448 · Incubator · APIs / Libraries

The Vector API incubates a sixth time.

Still incubating in Java 25 (JEP 508).

OpenJDK JEP 448 (leaves JavaMastery)

JVM / Runtime

Also in this release: deprecation of the Windows 32-bit x86 port (JEP 449) and preparation to disallow dynamic agent loading (JEP 451).

Examples

  • versions6Java21Features.javaJava 21 example · Requires Java 21Virtual threads, record patterns, pattern matching for switch, and sequenced collections, in their Java 21 final form. String templates, structured concurrency, and scoped values were previews and are not shown.
  • concurrency6VirtualThreadsDemo.javaJava 21 example · Requires Java 21Virtual threads in the concurrency chapter. Thread.ofVirtual is the final Java 21 API. In Java 21 a virtual thread can stay pinned inside synchronized; Java 24 changed that.
  • advconcurrency7StructuredConcurrency.javaStable demo; Java 25 preview API is only in the comment · Requires Java 8The main method uses CompletableFuture and runs without --enable-preview. The comment shows the Java 25 preview API: StructuredTaskScope.open with a Joiner. JEP 505 is still a preview.

Related reading

Historical notes

Since Java 17, this release finalizes virtual threads, switch patterns, and record patterns, and it adds sequenced collections. Structured concurrency, scoped values, string templates, unnamed variables, and unnamed classes remain previews. The Java 21 source file is unchanged and does not demonstrate the preview APIs.

Sources. General availability, JSR 396, and the JEP list: OpenJDK JDK 21. The example file versions6Java21Features.java is unchanged. OpenJDK JDK 21 (leaves JavaMastery)

All versions