JDK 17: Java 17 New Features – Analysis and Explanations
JDK 17 (Java 17) has been released and it comes packed with some exciting new features and improvements. In this article, we will explore the new features of JDK 17 and provide code examples to help you understand them.
1. Sealed classes and interfaces
Sealed classes and interfaces allow you to restrict the inheritance hierarchy of a class or interface to a specific set of subclasses. This can help improve code maintainability and make it easier to reason about the behavior of code. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public sealed class Shape permits Circle, Square, Triangle { // ... } public final class Circle extends Shape { // ... } public final class Square extends Shape { // ... } public final class Triangle extends Shape { // ... } |
In this example, the Shape
class is declared as sealed
, with Circle
, Square
, and Triangle
as permitted subclasses. This means that any other subclasses of Shape
would be disallowed.
2. Pattern matching for switch statements
Pattern matching for switch statements can simplify code that checks the type of an object or its properties. Here’s an example:
1 2 3 4 5 6 7 8 9 10 |
Object obj = // some object switch (obj) { case String s -> System.out.println("String: " + s); case Integer i && i > 0 -> System.out.println("Positive integer: " + i); case List<String> list && !list.isEmpty() -> System.out.println("Non-empty list: " + list); default -> System.out.println("Other object: " + obj); } |
In this example, the switch statement checks the type and properties of the obj
object using pattern matching. Depending on the type and properties of obj
, the appropriate case is executed.
3. Enhanced pseudo-random number generators
JDK 17 includes several new algorithms for generating pseudo-random numbers. Here’s an example using the Xoshiro256StarStar
algorithm:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Random; public class RandomExample { public static void main(String[] args) { Random random = new Random(); random.setSeed(1234); System.out.println(random.nextInt()); } } |
4. Enhanced support for Unicode 13.0.0
JDK 17 includes updated support for Unicode 13.0.0, which includes several new characters and scripts.
5. Foreign Function and Memory API (Incubator)
This feature provides a standardized API for accessing foreign functions and memory in other programming languages, such as C and C++. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import jdk.incubator.foreign.CLinker; import jdk.incubator.foreign.FunctionDescriptor; import jdk.incubator.foreign.MemoryAddress; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; public class CMathExample { public static void main(String[] args) throws Throwable { var math = CLinker.getInstance().lookup("math", FunctionDescriptor.of(CLinker.C_LONG, CLinker.C_DOUBLE)); MethodHandle mathHandle = math.asHandle(); double x = 3.14159; MemoryAddress xAddress = CLinker.toCString(x); long result = (long) mathHandle.invokeExact(xAddress); System.out.println(result); } } |
In this example, we use the CLinker
class to call a function from the C math library. The toCString
method is used to convert the double
value to a MemoryAddress
that can be passed to the C function.
6. Enhanced NullPointerException messages
JDK 17 includes improved NullPointerException messages, making it easier to debug null-related issues in your code. Here’s an example:
1 2 3 4 |
String s = null; System.out.println(s.length()); |
In this example, s
is null and calling length()
on it would result in a NullPointerException. With JDK 17, the message of the exception includes the name of the variable that caused the exception:
1 2 3 |
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null |
This can make it easier to identify the root cause of the issue.
7. Enhanced support for macOS
JDK 17 includes enhanced support for macOS, including improved performance and support for Apple Silicon. This can help Java developers on macOS to write and run Java code more efficiently.
In conclusion, JDK 17 brings several exciting new features and improvements to Java. By using these features, you can write cleaner and more maintainable code, and take advantage of improved performance and support on macOS.