Java 21: A Major Milestone for the Platform
Java 21, released in September 2023, is a Long-Term Support (LTS) release — meaning it will receive security patches and updates for years to come. It's one of the most feature-rich Java releases in recent memory, bringing several capabilities out of preview and into stable, production-ready status. Here's what every Java developer needs to know.
Virtual Threads (Project Loom) — JEP 444
Virtual threads are arguably the biggest addition in Java 21. Unlike traditional platform threads (which are tied to OS threads and expensive to create), virtual threads are lightweight threads managed by the JVM. You can create millions of them without running out of memory.
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 10_000).forEach(i ->
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1));
return i;
})
);
}
This is transformative for I/O-heavy applications (web servers, database clients) where threads spend most of their time waiting. Virtual threads make high-concurrency Java apps far simpler to write compared to reactive programming models.
Sequenced Collections — JEP 431
Java 21 introduces three new interfaces — SequencedCollection, SequencedSet, and SequencedMap — to provide a unified API for ordered collections. Previously, accessing the first or last element of a list required different patterns for different types.
List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Carol"));
names.getFirst(); // "Alice"
names.getLast(); // "Carol"
names.reversed(); // ["Carol", "Bob", "Alice"]
Record Patterns — JEP 440
Building on pattern matching for instanceof (introduced earlier), record patterns let you deconstruct record objects directly in pattern matching expressions.
record Point(int x, int y) {}
Object obj = new Point(3, 7);
if (obj instanceof Point(int x, int y)) {
System.out.println("x=" + x + ", y=" + y);
}
This makes working with data-carrier classes cleaner and more expressive.
Pattern Matching for switch — JEP 441
Switch expressions can now match on types, not just values. Combined with guarded patterns, this enables very readable branching logic:
String format(Object obj) {
return switch (obj) {
case Integer i when i < 0 -> "Negative integer: " + i;
case Integer i -> "Positive integer: " + i;
case String s -> "String: " + s;
default -> "Other: " + obj;
};
}
String Templates — JEP 430 (Preview)
Java 21 includes String Templates as a preview feature, offering a safer and more readable alternative to string concatenation and String.format():
String name = "World";
String greeting = STR."Hello, \{name}!"; // "Hello, World!"
Should You Upgrade to Java 21?
| Consideration | Recommendation |
|---|---|
| New projects | ✅ Start with Java 21 (LTS) |
| On Java 17 LTS | ✅ Plan migration — major gains available |
| On Java 11 LTS | ✅ Prioritise upgrade — many improvements missed |
| On Java 8 | ⚠️ Critical to modernise — support window narrowing |
Conclusion
Java 21 is a generational leap. Virtual threads alone justify the upgrade for any application that handles concurrent I/O. Combined with pattern matching improvements, sequenced collections, and record patterns, Java 21 makes the language more expressive and performant than ever. If you're planning a new Java project, there's no better version to start with.