What’s New in Java 21? Explore 15 JEPs, Preview Features, and Performance Boosts
Java 21, the latest long‑term support release, brings 15 JEPs—including string templates, record patterns, virtual threads, and a foreign function API—along with extensive performance, security, and maintenance updates, while extending Java 11 support through 2032.
On September 19, Oracle announced the official release of Java 21, the 12th version delivered on schedule after moving to a six‑month release cadence.
Java 21 is a long‑term‑support (LTS) release with at least eight years of Oracle support, and Oracle also pledged to extend Java 11 support until January 2032.
In total, 24,196 JIRA issues were resolved for the Java 11‑to‑21 upgrade, with contributions from Oracle staff, individual developers, and major Chinese companies such as Tencent, Alibaba, Huawei, and Loongson.
Key New Features in Java 21
Java 21 introduces 15 JEPs, including six preview features and one incubating feature.
Project Amber
JEP 430: String Templates (Preview) simplifies the creation of runtime‑evaluated strings, improves readability of mixed text‑and‑expression strings, enhances security when building strings from user input, allows custom formatting syntax, and supports non‑string value computation.
<span>String name = "Joan";</span>
<span>String info = STR."My name is \{name}"; // template expression</span>
<span>assert info.equals("My name is Joan");</span>Compile and run with preview flags:
javac --release 21 --enable-preview Test.java</code>
<code>java --enable-preview TestProject Loom
JEP 444: Virtual Threads adds lightweight virtual threads to the platform, allowing massive concurrency with far fewer OS threads and minimal code changes.
<span>void handle(Request request, Response response) {</span>
<span> var url1 = ...;</span>
<span> var url2 = ...;</span>
<span> try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {</span>
<span> var future1 = executor.submit(() -> fetchURL(url1));</span>
<span> var future2 = executor.submit(() -> fetchURL(url2));</span>
<span> response.send(future1.get() + future2.get());</span>
<span> } catch (ExecutionException | InterruptedException e) {</span>
<span> response.fail(e);</span>
<span> }</span>
<span>}</span>
<span>String fetchURL(URL url) throws IOException {</span>
<span> try (var in = url.openStream()) {</span>
<span> return new String(in.readAllBytes(), StandardCharsets.UTF_8);</span>
<span> }</span>
<span>}</span>JEP 446: Scoped Values (Preview) introduces ScopedValue<T> for safe, efficient data sharing across many virtual threads, outperforming thread‑local variables.
final static ScopedValue<...> V = ScopedValue.newInstance();
ScopedValue.where(V, <value>).run(() -> { ... V.get() ... });JEP 453: Structured Concurrency (Preview) provides an API that treats related tasks as a single unit, simplifying error handling, cancellation, and observability.
Project Panama
JEP 442: Foreign Function & Memory API (3rd Preview) enables Java code to call native libraries and access off‑heap memory safely, avoiding JNI pitfalls.
JEP 448: Vector API (6th Incubator) adds VectorSpecies<E> to express vector computations that compile to optimal CPU vector instructions.
Core Library Enhancements
JEP 431: Sequenced Collections introduces the SequencedCollection<E> interface, providing a well‑defined iteration order and convenient access to first/last elements.
Performance Updates
JEP 439: Generational ZGC extends ZGC with generational collection, improving performance by more frequently collecting young objects.
JEP 452: Key Encapsulation Mechanism API adds a KEM class for public‑key encryption of symmetric keys.
Maintenance and Deprecations
JEP 449: Deprecate 32‑bit x86 Port for Removal marks the Windows 32‑bit x86 build as deprecated for future removal.
JEP 451: Prepare to Disallow Dynamic Loading of Agents warns users when agents are loaded dynamically, paving the way for a future prohibition.
Download the JDK from the official Oracle site:
https://www.oracle.com/java/technologies/downloads/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
