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)
Introduced through JEP 409, sealed classes provide more control over inheritance by allowing a class or interface to explicitly declare which classes are permitted to extend or implement it. Sealed classes provide more control over inheritance by allowing a class or interface to explicitly declare which classes are permitted to 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.
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 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.
Subscribe to my newsletter
Read articles from Noel KAMPHOA directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Noel KAMPHOA
Noel KAMPHOA
I am a computer engineer with more than ten years of practice. I worked for almost 9 years as a consultant on payroll processing and salary calculation software. I have solid experience in setting up tax rules related to salary processing. I also have some experience in the banking field, with some banking system migration missions. I recently started writing because I have always had a passion for transmitting knowledge. I have been a mentor for several years on platforms like CodeMentor and Openclassrooms.