Java 21

Repeat

StringBuilder sb = new StringBuilder();
sb.append("Hello").repeat(" world", 5);
System.out.println(sb.toString());
Hello world world world world world

Math Clamp

System.out.println(Math.clamp(23, 25, 30));
25

Pattern Matching for Instaceof

With the new instanceof operation, you can validate type and cast to type with just one line:

Object numberObject = Integer.parseInt("1");

if (numberObject instanceof Integer i) {
    System.out.println(i.intValue());
}
1

Also it is supported by records:

var developer = new Developer("Alexandra", 8);
if (developer instanceof Developer(String name, int age)) {
    System.out.println(name + " " + age);
}

Switch Expression

Pattern Matching with Switch

switch (dev) {
    case Developer d -> System.out.println("I am a developer");
    default -> System.out.println("I don't know");
}
I m a developer

Type and Guarded Patterns

Object price = 1000;

String description = switch (price) {
    case Integer i when i > 900 -> "This is a big number " + i.toString();
    case Integer i -> "This is a normal number " + i.toString();
    case Long l -> "A long number " + l.toString();
    default -> price.toString();
};

System.out.println(description);
This is a big number 1000

Virtual Threads

From Java documentation:

A virtual thread still runs code on an OS thread. However, when code running in a virtual thread calls a blocking I/O operation, the Java runtime suspends the virtual thread until it can be resumed. The OS thread associated with the suspended virtual thread is now free to perform operations for other virtual threads.

try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    IntStream.range(0, 10).forEach(i -> {
    executor.submit(() -> {
        System.out.println(STR."{i}");
        });
    });
}
3
4
1
...

Key Encapsulation Mechanism

var kpg = KeyPairGenerator.getInstance("X25519");
var kp = kpg.generateKeyPair();

      // Sender
var kem1 = KEM.getInstance("DHKEM");
var sender = kem1.newEncapsulator(kp.getPublic());
var encapsulated = sender.encapsulate();
var k1 = encapsulated.key();

      // Receiver
var kem2 = KEM.getInstance("DHKEM");
var receiver = kem2.newDecapsulator(kp.getPrivate());
var k2 = receiver.decapsulate(encapsulated.encapsulation());

System.out.println(k2.equals(k1));
true