Mastering Java 17: Unveiling New Features with Comprehensive Code Examples

Tharusha Wijayabahu
3 min readMar 31, 2024

--

Part 1: Exploring Java 17 — Sealed Classes and Pattern Matching

Welcome to the first part of our deep dive into Java 17’s new features. This version marks a significant milestone in Java’s evolution, introducing enhancements that promise to improve developer productivity and application performance. In this segment, we’ll explore two key features: Sealed Classes and Pattern Matching for switch. These additions bring more expressiveness and control to the Java language, and we'll see exactly how with some practical code examples.

Sealed Classes: Precision in Inheritance

Sealed classes are a major highlight of Java 17, offering a way to control which classes can extend or implement them. This feature is especially useful in scenarios where a strict hierarchy is desired, making the code more maintainable and secure.

Why Sealed Classes?

Traditionally, once you declare a class in Java, any other class can extend it, leading to potentially unpredictable subclassing. Sealed classes restrict this by allowing class authors to define a specific set of classes that can extend or implement them.

Code Example: Defining Sealed Classes

public sealed class Vehicle permits Car, Truck, Motorcycle {

private String registrationNumber;

// Vehicle class methods and properties
}

final class Car extends Vehicle {
// Car-specific implementations
}

final class Truck extends Vehicle {
// Truck-specific implementations
}

non-sealed class Motorcycle extends Vehicle {
// Motorcycle can be extended further
}

In this example, Vehicle is a sealed class, meaning only Car, Truck, and Motorcycle can extend it. Notice how Motorcycle is declared as non-sealed, allowing further extension, showcasing the flexibility sealed classes offer.

Pattern Matching for switch: Enhancing Readability and Safety

Pattern Matching for switch is another feature currently in preview in Java 17. It extends the switch statement, allowing it to be used more flexibly and safely when dealing with various types.

Why Pattern Matching?

Before Java 17, handling multiple types in a switch statement could be cumbersome and error-prone, often requiring explicit casting and type checks. Pattern Matching simplifies this, making the code more readable and eliminating the need for manual type validation.

Code Example: Using Pattern Matching in switch

Object obj = // Some object
String result = switch (obj) {
case Integer i -> "Integer with value " + i;
case String s && s.length() > 5 -> "Long String of length " + s.length();
case String s -> "String of length " + s.length();
default -> "Unknown Object";
};

This example illustrates how pattern matching can be used to handle different types and conditions within a switch statement. The second case demonstrates the use of a pattern with a guard (the && s.length() > 5 part), adding even more flexibility.

Conclusion

Java 17’s introduction of Sealed Classes and Pattern Matching for switch represents a significant advancement in the language's design, offering developers more control and clarity. By understanding and utilizing these features, you can write more concise, maintainable, and secure Java code.

Stay tuned for the second part of this series, where we’ll delve into other exciting features of Java 17, including new APIs, garbage collectors, and security enhancements, complete with practical code examples.

--

--

Tharusha Wijayabahu

Tharusha Wijayabahu: software engineer, tech enthusiast, and writer. Passionate about innovative solutions and sharing knowledge. more at https://bit.ly/3Evfzdf