Skip to main content

Command Palette

Search for a command to run...

Java 21 Features in Java

Author by Manikandan S - 23rd September 2026

Updated
•5 min read•View as Markdown
Java 21 Features in Java
M
Product Engineer with 3.4 years of experience in Java, Spring Boot, React, Vue, SQL, AWS, and Warehouse Management Systems. Passionate about backend development and software engineering.

Introduction

Java 21 is an LTS (Long-Term Support) version of Java. It introduced several important language, concurrency, and API improvements.

Important Java 21 features include:

  1. Virtual Threads

  2. Record Patterns

  3. Pattern Matching for switch

  4. Sequenced Collections

  5. String Templates

  6. Unnamed Patterns and Variables

  7. Unnamed Classes and Instance Main Methods

  8. Generational ZGC

  9. Key Encapsulation Mechanism API

  10. Scoped Values

  11. Structured Concurrency

  12. Foreign Function & Memory API


1. Virtual Threads

Virtual Threads are lightweight threads designed for high-throughput concurrent applications.

Syntax

Thread.startVirtualThread(() -> {
    // task
});

Example

public class Main {
    public static void main(String[] args) {

        Thread.startVirtualThread(() -> {
            System.out.println("Virtual Thread Running");
        });
    }
}

Output:

Virtual Thread Running

2. Record Patterns

Record Patterns allow us to extract values directly from a record.

Example

record Student(String name, int age) {}

public class Main {
    public static void main(String[] args) {

        Student student = new Student("Mani", 26);

        if (student instanceof Student(String name, int age)) {
            System.out.println(name);
            System.out.println(age);
        }
    }
}

3. Pattern Matching for switch

Java 21 allows switch to work with patterns.

public class Main {
    public static void main(String[] args) {

        Object value = "Java";

        String result = switch (value) {
            case Integer i -> "Integer: " + i;
            case String s -> "String: " + s;
            default -> "Other";
        };

        System.out.println(result);
    }
}

Output:

String: Java

4. Sequenced Collections

Java 21 introduced interfaces for collections with a defined encounter order.

Important interfaces:

  • SequencedCollection

  • SequencedSet

  • SequencedMap

Example

import java.util.ArrayList;
import java.util.SequencedCollection;

public class Main {
    public static void main(String[] args) {

        SequencedCollection<String> names =
                new ArrayList<>();

        names.add("Java");
        names.add("Spring");
        names.add("React");

        System.out.println(names.getFirst());
        System.out.println(names.getLast());
    }
}

Output:

Java
React

5. String Templates

String Templates were introduced as a preview feature in Java 21.

They provide a way to combine Strings and expressions.

Note: String Templates were a preview feature in Java 21, not a permanent Java 21 feature.

Example:

String name = "Java";

// Preview feature
String message = STR."Hello \{name}";

System.out.println(message);

Output:

Hello Java

6. Unnamed Patterns and Variables

Java 21 introduced unnamed patterns and variables as a preview feature.

They use _ when a variable is not needed.

if (obj instanceof String _) {
    System.out.println("It is a String");
}

This was a preview feature in Java 21.


7. Unnamed Classes and Instance Main Methods

Java 21 introduced simplified ways to write small Java programs as a preview feature.

Example:

void main() {
    System.out.println("Hello Java 21");
}

This reduces the boilerplate needed for simple programs.

This was a preview feature in Java 21.


8. Generational ZGC

Generational ZGC improves the Z Garbage Collector by separating objects into young and old generations.

It is designed to improve application performance and memory management.


9. Scoped Values

Scoped Values provide a way to share immutable data within a bounded execution scope, especially with concurrent code.

Example concept:

static final ScopedValue<String> USER =
        ScopedValue.newInstance();

Scoped Values were introduced as a preview feature in Java 20 and continued as a preview feature in Java 21.


10. Structured Concurrency

Structured Concurrency provides an approach for managing multiple concurrent tasks as a single unit.

Example:

try (var scope =
        new StructuredTaskScope.ShutdownOnFailure()) {

    // Start concurrent tasks

    scope.join();
}

Structured Concurrency was a preview feature in Java 21.


11. Foreign Function & Memory API

The Foreign Function & Memory API allows Java programs to interact with code and memory outside the JVM without traditional JNI.

Example API classes include:

Arena
MemorySegment
Linker
SymbolLookup

The API became a permanent feature in Java 22; Java 21 provided its final preview/incubation path toward that release.


12. Key Encapsulation Mechanism API

Java 21 introduced the Key Encapsulation Mechanism (KEM) API for cryptographic key encapsulation.

Example:

KEM kem = KEM.getInstance("DHKEM");

It is mainly used in advanced cryptographic applications.


13. Java 21 Feature Summary

Feature Status / Purpose
Virtual Threads Final
Record Patterns Final
Pattern Matching for switch Final
Sequenced Collections Final
String Templates Preview
Unnamed Patterns & Variables Preview
Unnamed Classes & Instance Main Preview
Generational ZGC Final
Scoped Values Preview
Structured Concurrency Preview
Foreign Function & Memory API Final preview in Java 21
KEM API Final

Conclusion

Java 21 is an important LTS release with major improvements in concurrency, pattern matching, collections, and developer productivity.

The most important Java 21 concepts are:

  • Virtual Threads

  • Record Patterns

  • Pattern Matching for switch

  • Sequenced Collections

  • Generational ZGC

  • Scoped Values

  • Structured Concurrency

  • String Templates

  • Unnamed Patterns and Variables

  • Unnamed Classes and Instance Main Methods

  • Foreign Function & Memory API

  • KEM API

Java 21 is widely relevant for modern Java, Spring Boot, backend development, microservices, and concurrent applications.