What is New in Java 21
Java 21 is a Long-Term Support (LTS) release that delivers major improvements in developer productivity and application performance. It finalizes several powerful features like pattern matching, record patterns, and virtual threads, while introducing new language constructs such as sequenced collections and string templates (preview). These changes make code simpler, safer, and more scalable.
Virtual Threads (Standard)
Virtual threads, previously known as Project Loom, are now a permanent part of the JDK. They allow millions of lightweight threads to run on a small number of platform threads, greatly simplifying high-concurrency applications without needing complex async frameworks.
Thread thread = Thread.ofVirtual().name("worker").start(() -> {
// Your task here
});
thread.join();
Structured concurrency (preview) helps manage groups of related virtual threads as a unit, making error handling and cancellation much easier.
Pattern Matching Enhancements
Record Patterns (Standard)
You can now deconstruct record values directly in pattern matching, making code much cleaner when working with data objects.
if (obj instanceof Point(int x, int y)) {
System.out.println(x + y);
}
Pattern Matching for switch (Standard)
Switch expressions and statements now support type patterns, guarded patterns, and null handling, reducing boilerplate and improving readability.
String result = switch (obj) {
case String s when s.length() > 5 -> "Long string";
case String s -> "Short string";
case Integer i -> "Number: " + i;
default -> "Other";
};
New Language Features (Preview)
String Templates (Preview)
A safer and more flexible way to embed expressions in strings, with processor-based validation and transformation.
String name = "Alice";
String greeting = STR."Hello \{name}! Today is \{new Date()}.";
Unnamed Patterns and Variables (Preview)
Use underscore (_) to indicate values that are intentionally unused, reducing warnings and clarifying intent.
if (obj instanceof Point(_, int y)) {
// Only care about y
}
Sequenced Collections (Standard)
A new hierarchy of interfaces adds consistent methods to access the first and last elements of ordered collections like List, Deque, and SortedSet.
SequencedCollection<String> coll = new ArrayList<>();
coll.addFirst("first");
coll.addLast("last");
String first = coll.getFirst();
This eliminates the need to cast or use different APIs depending on the collection type.
Key API and Library Updates
- New
java.util.concurrentAPIs for structured concurrency (preview) - Improved
Foreign Function & Memory API(third preview) for safer native code access Vector API(sixth incubator) for high-performance vector computations- Enhanced deprecation annotations with
@Deprecated(forRemoval = true)now standard - Generational mode enabled by default in ZGC for better performance
Removed and Deprecated Features
| Feature | Status |
|---|---|
| Legacy SecurityManager | Deprecated for removal (planned for future release) |
| Old finalize() method | Further deemphasized |
| 32-bit x86 ports | Removed |
| Experimental AOT and JIT compiler (Jaotc) | Removed |
Performance and Tooling Improvements
Garbage collectors like ZGC and Shenandoah now enable generational mode by default, reducing latency and memory overhead. The JDK includes better support for containers, improved diagnostics, and enhanced tooling for virtual threads.
Why Upgrade to Java 21
As an LTS release, Java 21 receives long-term support and security updates. It combines mature, production-ready features like virtual threads and pattern matching with forward-looking previews. Developers can write more concise, readable, and scalable code while benefiting from significant performance gains in concurrent applications.