Overview of New Features in Java 21
Introduction
Java 21 was released on 19th September 2023. It is a long-term support (LTS) release, the successor to Java 17Click to read: Java 17, and one of the most significant releases in Java's history: virtual threads changed how concurrent Java code is written, and record patterns and pattern matching for switch completed the data-oriented programming features which started with records and sealed classes. This article gives an overview of the new features in Java 21 - its fifteen JDK Enhancement Proposals (JEPs) - and then summarizes all the major changes since Java 17, with links to detailed tutorials.
Final features in Java 21
Previews and incubators in Java 21
Upgrading from Java 17 to 21
- Virtual Threads - JEP 444 - lightweight threads managed by the JVM, so that simple blocking code can scale to millions of concurrent tasks. Read the virtual threads tutorialClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples.
- Record Patterns - JEP 440 - deconstruct records into their components, and nest patterns. Read about record patternsClick to read tutorial: Java 21 Record Patterns with Examples.
- Pattern Matching for switch - JEP 441 - type and record patterns in case labels,
whenguards,case null, and exhaustive switches over sealed types. Read about pattern matching for switchClick to read tutorial: Java 21 Pattern Matching for switch with Examples. - Sequenced Collections - JEP 431 -
getFirst(),getLast(),reversed()and more for every ordered collection. Read about sequenced collectionsClick to read tutorial: Java 21 Sequenced Collections Explained. - Generational ZGC - JEP 439 - ZGC splits the heap into young and old generations, using less memory and CPU while keeping sub-millisecond pauses. Read about generational ZGCClick to read tutorial: Java 21 Generational ZGC Explained.
- Key Encapsulation Mechanism API - JEP 452 -
javax.crypto.KEM, the building block for modern encryption protocols. See the Java 21 API additionsClick to read tutorial: Java 21 API Additions: StringBuilder.repeat, Math.clamp, which also coversStringBuilder.repeat(),Math.clamp()and more.
- Unnamed Patterns and Variables (Preview) - JEP 443 - the
_for unused variables and pattern components; final in Java 22. Read about unnamed variables and patternsClick to read: unnamed variables and patterns. - Unnamed Classes and Instance Main Methods (Preview) - JEP 445 - simpler programs with
void main(); final in Java 25 as compact source files and instance main methodsClick to read: compact source files and instance main methods. - String Templates (Preview) - JEP 430 - string interpolation with template processors. After a second preview in Java 22, the design was withdrawn in Java 23 for rework, so this feature does not exist in current Java.
- Scoped Values (Preview) - JEP 446 and Structured Concurrency (Preview) - JEP 453 - companions to virtual threads; scoped values became final in Java 25 - read the scoped values tutorialClick to read: scoped values tutorial.
- Foreign Function & Memory API (Third Preview) - JEP 442 - final in Java 22 - read the FFM API tutorialClick to read: FFM API tutorial; and the Vector API (Sixth Incubator) - JEP 448.
- Prepare to Disallow the Dynamic Loading of Agents - JEP 451 - loading a Java agent into a running JVM (as some profilers and mocking libraries do) now prints a warning; it can be allowed explicitly with
-XX:+EnableDynamicAgentLoading. Agents loaded at startup with-javaagentare unaffected. - Deprecate the Windows 32-bit x86 Port for Removal - JEP 449 - removed in Java 24.
Each release also has its own overview: Java 18Click to read tutorial: Overview of New Features in Java 18, Java 19Click to read tutorial: Overview of New Features in Java 19 and Java 20Click to read tutorial: Overview of New Features in Java 20.
- UTF-8 by default: on Windows especially, check code which reads or writes text without naming a charset.
- Dynamic agents: if tools attach agents at runtime, such as some profilers or mocking libraries in tests, add
-XX:+EnableDynamicAgentLoadingor load them with-javaagent. - Adopting virtual threads: for frameworks such as Spring Boot 3.2+, enable them with a configuration property; review code which uses
synchronizedaround slow blocking calls, which pins virtual threads in Java 21 (fixed in Java 24). - Modernizing code: switch-based pattern matching over sealed hierarchies and records can replace visitor patterns and long
if-instanceofchains.
Continue Reading Java 18 to 21 Tutorials
Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples Java 21 Pattern Matching for switch with ExamplesClick to read tutorial: Java 21 Pattern Matching for switch with Examples Java 21 Sequenced Collections ExplainedClick to read tutorial: Java 21 Sequenced Collections Explained Java 21 Generational ZGC ExplainedClick to read tutorial: Java 21 Generational ZGC Explained Java 21 API Additions: StringBuilder.repeat, Math.clampClick to read tutorial: Java 21 API Additions: StringBuilder.repeat, Math.clamp Overview of New Features in Java 20Click to read tutorial: Overview of New Features in Java 20 Overview of New Features in Java 18Click to read tutorial: Overview of New Features in Java 18 Overview of New Features in Java 19Click to read tutorial: Overview of New Features in Java 19
Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples Java 21 Pattern Matching for switch with ExamplesClick to read tutorial: Java 21 Pattern Matching for switch with Examples Java 21 Sequenced Collections ExplainedClick to read tutorial: Java 21 Sequenced Collections Explained Java 21 Generational ZGC ExplainedClick to read tutorial: Java 21 Generational ZGC Explained Java 21 API Additions: StringBuilder.repeat, Math.clampClick to read tutorial: Java 21 API Additions: StringBuilder.repeat, Math.clamp Overview of New Features in Java 20Click to read tutorial: Overview of New Features in Java 20 Overview of New Features in Java 18Click to read tutorial: Overview of New Features in Java 18 Overview of New Features in Java 19Click to read tutorial: Overview of New Features in Java 19