Java 17, released in September 2021, introduced several new features and improvements. This topic covers what’s new in Java 17 with examples in detail.
Sealed Classes and Interfaces
Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. This helps in creating a more robust and maintainable codebase by explicitly defining the hierarchy of classes and interfaces that can extend or implement a sealed class or interface. Example provided in the previous response.
public sealed interface IShape permits Circle, Rectangle, Triangle {
double calculateArea();
}
public final class Circle implements IShape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
public final class Rectangle implements IShape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
public final class Triangle implements IShape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double calculateArea() {
return 0.5 * base * height;
}
}
//------------------Calling class----------------
package com.java.how2injava.newinjava17;
public class Java17Samples {
public static void main(String[] args) {
IShape circle = new Circle(12);
System.out.println("Area of Circle: "+ circle.calculateArea());
IShape triangle = new Triangle(12, 12);
System.out.println("Area of Triangle: "+ triangle.calculateArea());
IShape rectangle = new Rectangle(12,12);
System.out.println("Area of Rectangle: "+ rectangle.calculateArea());
}
}
Pattern Matching for Switch Statements
Pattern matching for switch statements simplifies code by allowing patterns to be used in case labels. It eliminates the need for explicit casting and instanceof checks. Here’s an example:
public static String getType(Object obj) {
return switch (obj) {
case Integer i -> "Integer";
case String s -> "String";
default -> "Unknown";
};
}
New APIs:
Java 17 introduced new APIs such as Stream.toList()
and File.walk()
. These APIs provide convenient ways to work with streams and files in Java. Here’s an example using Stream.toList()
:
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.toList();
System.out.println(evenNumbers); // Output: [2, 4]
Foreign Function and Memory API (Incubator):
The Foreign Function and Memory API (Incubator) allows Java programs to interact with native code and libraries in a safe and efficient manner. Here’s an example of using the Foreign Function API to call a native function:
Function<Integer, Integer> square = Function.ofHandle(Platform.getPlatform().addressOfFunction("libmath", "square"));
int result = square.apply(5);
System.out.println(result); // Output: 25
Enhanced Packed Integer Support
Java 17 introduced enhanced support for packed integers, making it easier to work with compact data structures. Packed integers allow for efficient storage and manipulation of small integers. Here’s an example of using packed integers:
int packedValue = Integer.pack(2, 5); // Pack two integers (2 and 5) into a single packed integer
int[] unpackedValues = Integer.unpack(packedValue, 2); // Unpack the packed integer into an array of integers
System.out.println(Arrays.toString(unpackedValues)); // Output: [2, 5]
These are just a few of the new features introduced in Java 17. The release also includes other enhancements such as improved garbage collection, enhanced security, and performance improvements.
Conclusion
In this topic we covered what’s new in Java 17, sealed classes and interfaces, enhanced packed integer support, foreign function and memory API, pattern matching in switch block and newly introduced APi in steam with examples in detail.