New Features in Java 17
Java 17, released on September 14, 2021, is a Long-Term Support (LTS) version. As an LTS release, it brings many significant features and updates. This article will introduce these new features and provide a download link for Java 17.
New Features in Java 17
Sealed Classes
Sealed classes allow classes or interfaces to restrict which other classes or interfaces can extend or implement them. This enhances Java’s type system, making it possible to create more predictable and secure type hierarchies.
Example:
// Declare a sealed class
public sealed class Shape
permits Circle, Rectangle, Square {
// Define properties and methods for Shape
}
// Circle class, extending Shape
final class Circle extends Shape {
private double radius;
// Constructor and methods
}
// Rectangle class, extending Shape
final class Rectangle extends Shape {
private double length;
private double width;
// Constructor and methods
}
// Square class, extending Shape
final class Square extends Shape {
private double side;
// Constructor and methods
}
public class Main {
public static void main(String[] args) {
// Use the sealed class
Shape shape = new Circle();
// ... perform operations
}
}
Pattern Matching for Switch
This feature extends the switch statement and expression to use pattern matching, making the code more concise and flexible.
Example:
Suppose we have a base class Shape
and several subclasses Circle
, Rectangle
, and Square
. We want to perform different operations based on the shape type.
First, define these classes:
abstract class Shape {}
final class Circle extends Shape {
final double radius;
Circle(double radius) { this.radius = radius; }
}
final class Rectangle extends Shape {
final double length, width;
Rectangle(double length, double width) { this.length = length; this.width = width; }
}
final class Square extends Shape {
final double side;
Square(double side) { this.side = side; }
}
Then, use pattern matching in the switch statement to handle these different shapes:
public class PatternMatchingDemo {
public static void main(String[] args) {
Shape shape = new Circle(10);
String result = switch (shape) {
case Circle c -> "Circle with radius: " + c.radius;
case Rectangle r -> "Rectangle with length: " + r.length + " and width: " + r.width;
case Square s -> "Square with side: " + s.side;
default -> "Unknown shape";
};
System.out.println(result);
}
}
In this example, the switch statement uses pattern matching to check the actual type of the shape
variable. For each type of Shape
(e.g., Circle
, Rectangle
, Square
), we define a different case to extract specific details and generate a corresponding string description.
Strong Encapsulation of JDK Internals
Java 17 continues to strengthen the encapsulation of JDK internal APIs to prevent them from being misused by external applications, thereby improving security and maintainability.
New macOS Rendering Pipeline
Java 17 introduces a new macOS rendering pipeline based on the Apple Metal API to enhance performance and user experience on macOS.
Removal and Deprecation of Features
Java 17 removes some old features, such as the Applet API, and deprecates others, such as the Security Manager.
Java 17 Download Links
Product / File Description | File Size | Download Link |
---|---|---|
Linux Arm 64 Compressed Archive | 172.37 MB | Download (sha256) |
Linux Arm 64 RPM Package | 172.62 MB | Download (sha256) |
Linux x64 Compressed Archive | 174.01 MB | Download (sha256) |
Linux x64 Debian Package | 149.40 MB | Download (sha256) |
Linux x64 RPM Package | 173.73 MB | Download (sha256) |
macOS Arm 64 Compressed Archive | 168.18 MB | Download (sha256) |
macOS Arm 64 DMG Installer | 167.60 MB | Download (sha256) |
macOS x64 Compressed Archive | 170.62 MB | Download (sha256) |
macOS x64 DMG Installer | 170.03 MB | Download (sha256) |
Windows x64 Compressed Archive | 172.42 MB | Download (sha256) |
Windows x64 Installer | 153.51 MB | Download (sha256) |
Windows x64 MSI Installer | 152.30 MB | Download (sha256) |