New Features of Java 23

Back to blogs

Introduction

Oracle Java Support Model

JDK 23 New Features


JDK 23 introduces a range of preview and incubating features, aimed at improving code expressiveness, modularity, concurrency, and performance. Most features are still in preview and not finalized. Key highlights include:

Enhanced Primitive Type Patterns:

Allows pattern matching with primitive types in instanceof and switch statements, making code cleaner and more efficient.

// Example of Enhanced Primitive Type Patterns
Object obj = 42;
switch (obj) {
    case Integer i -> System.out.println("Integer: " + i);
    case Long l    -> System.out.println("Long: " + l);
    default        -> System.out.println("Other: " + obj);
}

Flexible Constructor Bodies:

Permits statements before this() or super() calls in constructors, improving code organization.

// Example of Flexible Constructor Bodies
public class MyClass {
    private int value;
    public MyClass(int value) {
        int preInitialization = value * 2;  // Non-referential statement
        this.value = preInitialization;  // Initialization
    }
}

Simplified Module Imports:

Enables more flexible module dependency declarations, aiding large project management.

// Example of Simplified Module Imports
import module java.base;
public class Example {
    public static void main(String[] args) {
        Map> grouped = Stream.of("apple", "banana", "cherry")
            .collect(Collectors.groupingBy(s -> Character.toUpperCase(s.charAt(0))));
        System.out.println(grouped);
    }
}

Structured Concurrency:

Manages related concurrent tasks as a single unit, simplifying error handling and resource management.

// Example of Structured Concurrency
Response handle() throws ExecutionException, InterruptedException {
    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
        Supplier user = scope.fork(() -> findUser());
        Supplier order = scope.fork(() -> fetchOrder());

        scope.join()            // Join both subtasks
             .throwIfFailed();  // Propagate errors if any

        // Both subtasks have succeeded, compose their results
        return new Response(user.get(), order.get());
    }
}

Updated Class-File API:

Provides a new API for reading and writing Java class files, useful for tooling and frameworks.

//Code Example
ClassFile classFile = ClassFile.read(Paths.get("MyClass.class"));
List methods = classFile.methods();
for (Method method : methods) {
    System.out.println("Method name: " + method.name());
}

Stream Gatherers:

Enhances stream processing with custom intermediate operations for more efficient data collection.

// Example
List result = Stream.of("a", "b", "c", "d")
    .gather(s -> s.toUpperCase())
    .toList();
System.out.println(result);

Vector API Enhancements:

Continues to evolve, enabling high-performance vector computations for numerical applications.

// Example
FloatVector.Species SPECIES = FloatVector.SPECIES_256;
float[] array = { 1.0f, 2.0f, 3.0f, 4.0f };
FloatVector vector
    = FloatVector.fromArray(SPECIES, array, 0);
FloatVector result = vector.mul(2);
result.intoArray(array, 0);
System.out.println(Arrays.toString(array));

Generational ZGC:

Optimizes garbage collection by managing young and old generations separately, improving performance for large heaps.

Markdown Documentation Comments:

Markdown Documentation Comments:

/**
 * # Example
 *
 * This is a **Markdown** comment.
 *
 * - Item 1
 * - Item 2
 */
public class Example {
    // Class implementation
}

Conclusion