top of page
  • khangaonkar

JDK 21: New features in Java 21

JDK 21 was releases on 19 Sept 2021. As always a new suite of goodies is made available for the benefit of the Java programmer. Many of them are in preview. This is a very brief listing of the new features. Each of the features requires a blog of its own.

JEP 430: String Templates

This preview improves string literals and text with expressions and template processors. In the example STR is the java template processor available automatically to all code.

String name = ""Bob";
String greeting = STR."Hello world, \{name}";

JEP 431: Sequenced Collections

Prior to this, some collections did not have a order and others had their own way. This proposal tries to make the "encounter" order consistent across collections. Defines new interface for a standard way to get items from a collection.

For example,

interface SequencedCollection<E> extends Collection<E> {
    // new method
    SequencedCollection<E> reversed();
    // methods promoted from Deque
    void addFirst(E);
    void addLast(E);
    E getFirst();
    E getLast();
    E removeFirst();
    E removeLast();

JEP 439: Generational ZGC

Improves GC by maintaining generations of young and old objects. This enable the GC to efficiently collect young objects more frequently as young objects are more likely to die young.

JEP 440: Record patterns

This further improves record patterns introduced in JDK16. Record pattern match allows uses to write compact data oriented code without explicit casting

// jdk16
record MyData(int x, int y) {}

static void printSum(Object obj) {
    if (obj instanceof MyData m) {
        int x = m.x();
        int y = m.y();
// jdk21
static void printSum(Object obj) {
    if (obj instanceof MyData(int x, int y) {

JEP 441: Pattern matching for switch

Improves the pattern matching for switch. Allows code as follows

void doSomething(Object obj) {
    return switch (obj) {
        case Integer i -> System.out.println("Object is Integer");
        case Long l    -> System.out.println("Object is Long");
        case Double d  -> System.out.println("Object is Double");
        default        -> System.out.println("Object is "+obj.toString();

JEP 442: Foreign function and memory API

Third preview of an API to let java programs access functions and data outside the JVM. Supposed to be a significant improvement over JNI.

Linker linker          = Linker.nativeLinker();
SymbolLookup stdlib    = linker.defaultLookup();
MethodHandle radixsort = linker.downcallHandle(stdlib.find("radixsort"), ...);

JEP 443: Unnamed patterns and Unused variables

The preview tries to improve readability of code by matching records without the name or type.

JEP 444: Virtual Threads

Preview in previous releases virtual threads are official in JDK21. Virtual threads allow you to scale beyond the limitations of OS threads while preserving the 1 thread per request usage pattern. Traditional java threads map to OS thread that take up more resources. Virtual threads scale by multiple virtual threads using a tradition thread when needed.

JEP 445: Unnamed classes and instance main methods

The purpose of the preview feature is to make to easy for programmers to get started with Java without knowing advanced features. Consider the code

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello world");

To understand the code you need to know what public is , what class is, what static is and so on. With this feature, you could write the same program with an instance main method ad

class Hello {
    main() {
        System.out.println("Hello world");

or even better using unnamed classes

void main() {
        System.out.println("Hello world");

JEP 446: Scoped Values

This preview feature allows for sharing of immutable values between threads. An improvement over thread local storage.

JEP 448: Vector API

A vector API that compiles to take advantage of Vector operations on supported CPU architectures for more efficient vector operations. The goal is to achieve better performance than comparable scalar operations.

JEP 451: Warnings during dynamic loading of agents

Agents can do dangerous things like altering the code. This proposal is preparing for the JDK to disallow loading of agents.

JEP 452: Key encapsulation mechanism

Key encapsulation secures symmetric keys using public key encryption. The prior technique used public keys to secure random symmetric keys using a public key. But this required padding and was not that secure. KEM uses properties of the public key to generate the symmetric key which is more secure. This adds support for KEM.

JEP 453: Structured Concurrency Preview feature that provides the ability for tasks that are running in different threads but are related to be treated as one unit for things such as cancellation , error handling and observability. When a method creates multiple tasks, structured concurrency reduces the burden on the programming for managing the lifecycle of sub tasks

In summary JDK21 introduces a number of new features and continues to make the language better. Many of these preview, which means, they are there to whetted by developers before formally adding them to the language.


Recent Posts

See All

Java record type was introduced as a preview in JDK 14 and made official in JDK 16. It is a simple feature but a really important one that should be used more often. This is short tutorial on the reco

bottom of page