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
  1. 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.
  2. 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.
  3. Pattern Matching for switch - JEP 441 - type and record patterns in case labels, when guards, 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.
  4. 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.
  5. 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.
  6. 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 covers StringBuilder.repeat(), Math.clamp() and more.
Previews and incubators in Java 21
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
Deprecations in Java 21
  1. 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 -javaagent are unaffected.
  2. Deprecate the Windows 32-bit x86 Port for Removal - JEP 449 - removed in Java 24.
Everything new from Java 17 to Java 21 For teams moving from Java 17 to Java 21, these are the main changes which became standard across the four releases -
AreaFeature (release)
LanguageRecord patternsClick to read tutorial: Java 21 Record Patterns with Examples (21), pattern matching for switchClick to read tutorial: Java 21 Pattern Matching for switch with Examples (21)
ConcurrencyVirtual threadsClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples (21), closeable ExecutorService and Future.resultNow() (19)
LibrariesSequenced collectionsClick to read tutorial: Java 21 Sequenced Collections Explained (21), StringBuilder.repeat, Math.clamp, KEM APIClick to read tutorial: Java 21 API Additions: StringBuilder.repeat, Math.clamp (21), HashMap.newHashMap() and Locale.of() (19)
Behaviour changesUTF-8 by defaultClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why (18), Thread.stop() disabled (20)
ToolsjwebserverClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial (18), @snippet in JavadocClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet (18)
Garbage collectionGenerational ZGCClick to read tutorial: Java 21 Generational ZGC Explained (21)
DeprecatedFinalizationClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead (18), URL constructors (20), dynamic agent loading warned (21)
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.
Upgrading from Java 17 to 21
  • 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:+EnableDynamicAgentLoading or 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 synchronized around 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-instanceof chains.
Summary In this article we went through the new features of Java 21, an LTS release. It finalized virtual threads, record patterns, pattern matching for switch, sequenced collections, generational ZGC and the KEM API, and previewed unnamed variables, simpler main methods, string templates (later withdrawn), scoped values and structured concurrency. We then summarized everything that became standard from Java 18 to 21, and the main points to check when upgrading from Java 17. Next, continue with the new features in Java 22Click to read: new features in Java 22.