TechStackTutor Logo
HOMEBLOGKIDSABOUT USCONTACT USBOOK DEMO

Top 10 Java 21 Features

By TechStackTutor • June 2025

Java 21 Features

1. Virtual Threads (Project Loom)

Old way: Old Java apps used platform threads — heavy and limited. High concurrency needed async frameworks.

Java 21: Java 21 adds Virtual Threads — lightweight, JVM-managed. Same Thread API now scales massively.

java
Thread.startVirtualThread(() -> { System.out.println("Hello from virtual thread!"); });

Benefit: Write simple, scalable apps — REST APIs can handle 1 million+ concurrent requests.

2. String Templates (Preview)

Old way: String building used + operators or String.format() — messy and error-prone.

Java 21: New STR syntax allows clean, safe string interpolation.

java
String name = "Alice"; String message = STR."Hello, \{name}!";

Benefit: Cleaner and more secure strings — great for building dynamic content.

3. Record Patterns

Old way: Had to unpack record fields manually via getters.

Java 21: Now you can destructure records in instanceof or switch.

java
record Point(int x, int y) {} if (obj instanceof Point(int x, int y)) { System.out.println("x=" + x + ", y=" + y); }

Benefit: Less boilerplate — code is more readable and expressive.

4. Pattern Matching for Switch

Old way: Switch only worked on constants (enums, ints). Complex type-based logic needed verbose if-else.

Java 21: Switch now supports type patterns and conditions.

java
switch (obj) { case String s -> System.out.println("String: " + s); case Integer i -> System.out.println("Integer: " + i); }

Benefit: Simpler, more powerful pattern-based logic — less boilerplate.

5. Unnamed Patterns & Variables

Old way: You had to declare variables even if you didn’t use them.

Java 21: New _ wildcard lets you ignore unused variables.

java
if (obj instanceof SomeClass(_, int y)) { System.out.println("y = " + y); }

Benefit: Code is cleaner — avoids clutter from unused variables.

6. Sequenced Collections

Old way: Collections had unclear ordering — inconsistent access to first/last.

Java 21: New SequencedCollection, SequencedMap interfaces provide clear order.

java
SequencedCollection<String> sc = ...; String first = sc.getFirst(); String last = sc.getLast();

Benefit: Better control over ordered data — makes APIs more predictable.

7. Key Encapsulation Mechanism (KEM) API

Old way: Secure key exchange required complex or custom code.

Java 21: Built-in KEM API handles modern crypto key exchange.

java
// KEM API usage — example coming soon

Benefit: Secure key exchange is easier and standardized — better crypto hygiene.

8. Scoped Values

Old way: ThreadLocal used for passing data — not safe for Virtual Threads.

Java 21: ScopedValue — safe, fast alternative for immutable data across threads.

java
ScopedValue<String> user = ScopedValue.newInstance(); ScopedValue.where(user, "Alice").run(() -> { System.out.println("User: " + user.get()); });

Benefit: Safe per-request context — works perfectly with Virtual Threads.

9. Foreign Function & Memory API

Old way: JNI was unsafe and verbose — hard to call native code.

Java 21: Final FFM API — easy and safe native access.

java
try (MemorySegment segment = Arena.ofAuto().allocate(100)) { // use native memory }

Benefit: Call C code and manage memory — fast and safe — no JNI needed.

10. Deprecations & Cleanups

Old way: Legacy APIs cluttered code, risked accidental use.

Java 21: Java 21 deprecates old APIs and cleans up internals.

java
// No code — platform improvement

Benefit: Keeps Java modern, safer and faster — prepares for future features.

← Back to Blog