You are currently viewing Java 17 New Features

Java 17 New Features

1. Introduction

Java 17 is a Long-Term Support (LTS) release packed with powerful enhancements that make Java more expressive, secure, and performant. It is defined by the official platform specification: JSR 392 – Java SE 17 Platform Specification. In this article, we highlight some of the key features introduced in JDK 17 that every developer should explore.

2. Sealed Classes (JEP 409)

JEP 409 introduced sealed classes, which give developers more control over inheritance. With sealed classes, a class or interface explicitly specifies which other classes can extend or implement it.

public abstract sealed class Shape permits Circle, Rectangle {}
final class Circle extends Shape {}
final class Rectangle extends Shape {}

Why it matters: This improves code security, modeling precision, and compiler optimizations.

3. Pattern Matching for switch (Preview) (JEP 406)

As outlined in JEP 406, pattern matching enhances the switch expression to allow matching based on type patterns.

static String formatter(Object o) {
    return switch (o) {
        case Integer i -> "Integer: " + i;
        case String s  -> "String: " + s;
        default        -> "Unknown type";
    };
}

Important: This is a preview feature in JDK 17. To use it, you must enable preview features in your local setup. However, this feature is fully available in Java 21 and later.

Why it matters: More concise and readable code, especially when using instanceof and casting.

4. Enhanced Pseudo-Random Number Generators (JEP 356)

JEP 356 introduced a new set of interfaces and implementations for random number generation that provide more flexibility and stream-based usage. A new set of interfaces and implementations for random number generation provides more flexibility and stream-based usage.

import java.util.random.RandomGenerator;

RandomGenerator generator = RandomGenerator.of("L64X256MixRandom");
int number = generator.nextInt();

Why it matters: Better support for reproducibility and multiple algorithms.

5. Strong Encapsulation of JDK Internals (JEP 403)

With JEP 403, internal elements of the JDK are now strongly encapsulated, preventing illegal reflective access to internal APIs. By strongly encapsulating internal elements of the JDK, this feature prevents illegal reflective access to internal APIs.

// Example: Illegal access like this is now blocked without --add-opens
// sun.misc.Unsafe unsafe = Unsafe.getUnsafe(); // Throws exception

Why it matters: Promotes better modularization and forces the use of supported APIs.

6. Context-Specific Deserialization Filters (JEP 415)

JEP 415 enables dynamic selection of serialization filters per context, significantly improving security. This feature enables dynamic selection of serialization filters per context, improving security.

public static void demoDeserializationFilter() throws Exception {
    // Serialize an object to bytes
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject("Hello from nkamphoa.com"); // Any simple serializable object
    oos.close();

    // Now deserialize with a filter
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    ObjectInputFilter filter = ObjectInputFilter.Config.createFilter("java.base/*;!*Array;!*Queue;maxdepth=5");
    ois.setObjectInputFilter(filter);
    Object obj = ois.readObject();
    System.out.println("Deserialized: " + obj);
}

Why it matters: Helps prevent deserialization attacks and adds fine-grained control over object input streams.

7. Conclusion

JDK 17 continues Java’s tradition of steady evolution while offering significant improvements to the language and standard APIs. These features help write cleaner, safer, and more efficient Java code. Explore them and keep your Java skills fresh!

You can find the complete code of this article here in GitHub.

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.