Java 22 introduces several innovative features and enhancements designed to improve developer productivity, performance, and ease of use. Below is a comprehensive overview of these new features, explained both technically and in layman's terms, along with code examples where applicable.

JDK 22 delivers 12 enhancements that are significant enough to warrant their own JDK Enhancement Proposals (JEPs), including seven preview features and one incubator feature. They cover improvements to the Java Language, its APIs, its performance, and the tools included in the JDK.

Language Improvements

Language Preview


1. Statements before super(...) [Preview] - JEP 447

Code Example

class Parent {
    Parent(int value) {
        System.out.println("Parent constructor: " + value);
    }
}

class Child extends Parent {
    Child(int value) {
        if (value <= 0) {
            throw new IllegalArgumentException("Value must be positive");
        }
        super(value);
        System.out.println("Child constructor");
    }
}

public class Main {
    public static void main(String[] args) {
        new Child(5);
    }
}

2. String Templates (Second Preview) - JEP 459

Code Example

String name = "Alice";
int age = 30;
String message = STR."Hello, my name is \{name} and I am \{age} years old.";
System.out.println(message);

3. Implicitly Declared Classes and Instance Main Methods (Second Preview) - JEP 463

Code Example

public class Main {
    void main() {
        System.out.println("Hello, world!");
    }
}

Libraries


1. Foreign Function & Memory API - JEP 454

Code Example

import jdk.incubator.foreign.*;
import static jdk.incubator.foreign.CLinker.*;
public class Main {
    public static void main(String[] args) {
        try (ResourceScope scope = ResourceScope.newConfinedScope()) {
            MemorySegment memory = MemorySegment.allocateNative(100, scope);
            memory.set(JAVA_INT, 0, 42);
            int value = memory.get(JAVA_INT, 0);
            System.out.println("Value: " + value);
        }
    }
}

Library Previews and Incubator


1. Class-File API (Preview) - JEP 457

Code Example

import jdk.internal.classfile.*;
public class Main {
    public static void main(String[] args) throws Exception {
        ClassFile cf = ClassFile.read("MyClass.class");
        System.out.println("Class Name: " + cf.name());
    }
}

2. Stream Gatherers (Preview) - JEP 461

Code Example

Stream<Integer> stream1 = Stream.of(1, 2, 3);
Stream<Integer> stream2 = Stream.of(4, 5, 6);
Stream<Integer> gatheredStream = Stream.of(stream1, stream2).flatMap(s -> s);
gatheredStream.forEach(System.out::println);

3. Structured Concurrency (2nd Preview) - JEP 462

Code Example

import java.util.concurrent.*;
public class Main {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        try (var executor = new StructuredTaskScope.ShutdownOnFailure()) {
            Future<Integer> future1 = executor.fork(() -> 1);
            Future<Integer> future2 = executor.fork(() -> 2);
            executor.join();
            System.out.println("Results: " + future1.resultNow() + ", " + future2.resultNow());
        }
    }
}

4. Scoped Values (2nd Preview) - JEP 464

Code Example

import jdk.incubator.concurrent.ScopedValue;
public class Main {
    static final ScopedValue<String> SCOPED_VALUE = ScopedValue.newInstance();
    public static void main(String[] args) {
        try (var scope = ScopedValue.enter(SCOPED_VALUE, "Hello")) {
            System.out.println(SCOPED_VALUE.get());
        }
    }
}

5. Vector API (7th Incubator) - JEP 460

Code Example

import jdk.incubator.vector.*;
public class Main {
    public static void main(String[] args) {
        VectorSpecies<Integer> SPECIES = IntVector.SPECIES_256;
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8};
        int[] b = {8, 7, 6, 5, 4, 3, 2, 1};
        int[] c = new int[8];

        IntVector va = IntVector.fromArray(SPECIES, a, 0);
        IntVector vb = IntVector.fromArray(SPECIES, b, 0);
        IntVector vc = va.add(vb);
        vc.intoArray(c, 0);

        for (int value : c) {
            System.out.println(value);
        }
    }
}

Performance


1. Regional Pinning for G1 - JEP 423

This feature is primarily a performance improvement within the JVM and does not have a direct code example.

Tooling


1. Launch Multi-File Source-Code Programs - JEP 458

Code Example

Given the files Main.java and Helper.java.

// Main.java
public class Main {
    public static void main(String[] args) {
        Helper.greet();
    }
}
// Helper.java
public class Helper {
    static void greet() {
        System.out.println("Hello from Helper!");
    }
}

You can run the program directly with,

java Main.java Helper.java.

Conclusion

JDK 22 introduces a range of features that enhance the language, libraries, performance, and tooling, making Java even more powerful and developer-friendly. Whether you're optimizing performance, simplifying concurrent programming, or leveraging new language features, JDK 22 has something valuable to offer.