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.


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}";

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();
}

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.


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();
        System.out.println(x+y);
    }
}
// jdk21
static void printSum(Object obj) {
    if (obj instanceof MyData(int x, int y) {
        System.out.println(x+y);
    }
}

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();
    };
}

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"), ...);
.
.
.
radixsort.invoke(....

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


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.


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");
}

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


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.


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


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

Go Tutorial: Receivers

When I was new to the Go programming language and saw this syntax func (m *Service) addNumbers(a int32, b int32) int32 I was confused. I had not seen syntax like (m *Service) between func and the func

JDK22: New features in Java 22

JDK 22 was released on March 19, 2024. 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

Go review: Should I use the Go programming language ?

Overview Go was developed by engineers at Google. Their primary motivation was dislike for C++. Goal was to create a language for systems programming. Its popularity has been slowly but steadily incre

Comentários


bottom of page