Java 21 introduces a plethora of new features and enhancements aimed at improving the the developer experience, performance, and scalability. This release is a significant step forward, bringing several long-awaited updates and experimental features that pave the way for the future of Java development. In this blog post, we will explore the most notable features of java 21, including virtual threads, pattern matching, foreign function and memory API, and more.
Introduction to Java 21
Java 21 continues the tradition ò regular, biannual updates to the Java platform. This release builds on the foundations laid by previous, introducing powerful new capabilities while maintaining the language’s core principles of readability, simplicity, and backward compartility.
Virtual Threads (Project Loom)
One of the most anticipated features in Java 21 is the introduction of virtual threads, part of Project Loom. Virtual threads are lightweight threads that make it easier to write, debug, and maintain concurrent applications.
Key Features of Virtual Threads
- Lightweight Concurrency: Virtual threads are much lighter than traditional platform threads, allowing applications to scale to millions of concurrent threads
- Simplified Concurrency: They provide a simpler programming model for concurrent applications, making it easier to write synchronous code that perform well.
public class VirtualThreadExample {
public static void main(String[] args) {
Thread vThread = Thread.ofVirtual().start(() -> {
System.out.println("Hello from a virtual thread!");
});
try {
vThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Using virtual Thread Executors
Java 21 provides a ExecutorService that create virtual threads, allowing you to manage and execute tasks in a more traditional manner using an executor framework.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VirtualThreadExecutorExample {
public static void main(String[] args) {
// Creating an ExecutorService that uses virtual threads
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
// Submitting tasks to the executor
executor.submit(() -> {
System.out.println("Task 1 running in virtual thread");
});
executor.submit(() -> {
System.out.println("Task 2 running in virtual thread");
});
// Shutdown the executor
executor.shutdown();
}
}
Pattern Matching for switch expressions
Java 21 extends pattern matching capabilities to switch expressions and statements, allowing more expressive and concise code.
Key Features
- Enhanced Readability: Simplifies complex condition logic.
- Type Safety: Ensures type checks at compile-time, reduce runtime errors
public class PatternMatchingExample {
public static void main(String[] args) {
Object obj = "Hello, Java 21!";
String result = switch (obj) {
case String s -> "String with length " + s.length();
case Integer i -> "Integer with value " + i;
default -> "Unknown type";
};
System.out.println(result);
}
}
Guarded Patterns
This example demonstrate the use of guarded patterns to add conditions to the pattern matching.
public class GuardedPatternExample {
public static void main(String[] args) {
Object obj = 42;
String result = switch (obj) {
case String s when s.length() > 5 -> "Long String: " + s;
case String s -> "Short String: " + s;
case Integer i when i > 10 -> "Large Integer: " + i;
case Integer i -> "Small Integer: " + i;
default -> "Unknown type";
};
System.out.println(result); // Outputs: Large Integer: 42
}
}
Nested Pattern Matching
This example shows how to use nested pattern matching to handle more complex scenarios
public class NestedPatternMatchingExample {
public static void main(String[] args) {
record Point(int x, int y) {}
record Circle(Point center, int radius) {}
record Rectangle(Point topLeft, Point bottomRight) {}
Object shape = new Circle(new Point(5, 5), 10);
String result = switch (shape) {
case Circle(Point center, int radius) -> "Circle with center at (" + center.x() + ", " + center.y() + ") and radius " + radius;
case Rectangle(Point topLeft, Point bottomRight) -> "Rectangle from (" + topLeft.x() + ", " + topLeft.y() + ") to (" + bottomRight.x() + ", " + bottomRight.y() + ")";
default -> "Unknown shape";
};
System.out.println(result); // Outputs: Circle with center at (5, 5) and radius 10
}
}
Pattern matching with Sealed Classes
This example demonstrates pattern matching with sealed classes, which provide a more controlled and secure to define class hierarchies
sealed interface Shape permits Circle, Rectangle, Triangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double length, double width) implements Shape {}
record Triangle(double base, double height) implements Shape {}
public class SealedClassPatternMatchingExample {
public static void main(String[] args) {
Shape shape = new Triangle(3.0, 4.0);
String result = switch (shape) {
case Circle c -> "Circle with radius " + c.radius();
case Rectangle r -> "Rectangle with length " + r.length() + " and width " + r.width();
case Triangle t -> "Triangle with base " + t.base() + " and height " + t.height();
};
System.out.println(result); // Outputs: Triangle with base 3.0 and height 4.0
}
}
Foreign Function and Memory API
The Foreign Function and Memory (FFM) API provides a safer and more efficient way to interact with native code and memory.
Key features
- Interoperability: Allows Java programs to call native libraries and manage native memory.
- Safety: Provides string guarantees around memory safety and resource management.
import java.lang.foreign.*;
public class ForeignFunctionExample {
public static void main(String[] args) {
try (MemorySegment segment = MemorySegment.allocateNative(1024)) {
MemoryAccess.setIntAtOffset(segment, 0, 42);
int value = MemoryAccess.getIntAtOffset(segment, 0);
System.out.println("Value: " + value);
}
}
}
Improve garbage collection
Java 21 includes several improvements to garbage collection (GC), enhancing performance and reducing latency for applications.
Key features
- ZGC and Shenandoah: continues enhancements to these low-latency garbage collectors.
- Automatic Tuning: Improved automatic tuning of GC parameters for better out-of-the-box performance.
Enhanced API and Language Features
Java 21 also brings various enhancements to existing APIs and language features.
Record Patterns
Record patterns allow more concise and readable code when working with record types.
public record Point(int x, int y) {}
public class RecordPatternExample {
public static void main(String[] args) {
Point point = new Point(3, 4);
if (point instanceof Point(int x, int y)) {
System.out.println("Point coordinates: " + x + ", " + y);
}
}
}
Conclusion
Java 21 is a landmark release that introduces powerful new features and improvements. Virtual threads simplify concurrent programming, pattern matching for switch expressions enhances code readability, and the foreign function and Memory API opens up new possibilities for native interoperability. These advancements, along with numerous other enhancement, make java 21 a compelling update for developers looking to leverage the latest innovations in Java ecosystem.