Java 17 Features
ā Java 17 Features (Released: September 2021)
Java 17 is a Long-Term Support (LTS) release, making it a very important milestone after Java 11.
1. š¹ Sealed Classes (Final ā JEP 409)
Sealed classes allow you to restrict which classes can extend or implement a class/interface.
ā Example:
public sealed class Shape permits Circle, Square {}
final class Circle extends Shape {}
final class Square extends Shape {}
ā This provides better control over inheritance and helps model domain logic securely.
2. š¹ Pattern Matching for instanceof
(Final ā JEP 394)
Allows type checking and casting in one step, reducing boilerplate.
ā Example:
Object obj = "Java 17";
if (obj instanceof String s) {
System.out.println(s.toUpperCase()); // JAVA 17
}
ā Improves readability and reduces errors.
3. š¹ Records (Final ā JEP 395)
Records are concise classes for immutable data, automatically generating constructors, toString()
, equals()
, and hashCode()
.
ā Example:
public record Person(String name, int age) {}
Person p = new Person("Aftab", 30);
System.out.println(p.name()); // Aftab
System.out.println(p); // Person[name=Aftab, age=30]
ā Excellent for DTOs and clean code.
4. š¹ Switch Expressions (Final ā JEP 361)
Now officially supported in Java 17, allowing switch
to return values.
ā Example:
String day = "MONDAY";
int result = switch (day) {
case "MONDAY", "FRIDAY" -> 6;
case "TUESDAY" -> 2;
default -> 0;
};
System.out.println(result); // 6
ā Safer and more expressive than old-style switch.
5. š¹ Text Blocks (Final ā JEP 378)
Multi-line string literals with consistent indentation and no need for \n
.
ā Example:
String json = """
{
"name": "Java",
"version": 17
}
""";
System.out.println(json);
ā Cleaner way to write HTML, SQL, JSON, etc.
6. š¹ New macOS
and Windows
Rendering Pipelines
- macOS: JEP 382 ā Rendering pipeline based on Metal.
- Windows: JEP 382/381 ā Legacy Direct3D replaced with OpenGL fallback and Metal.
š Internal enhancement; no change for developers unless working on UIs.
7. š¹ Foreign Function & Memory API (Incubator ā JEP 412)
A safe and efficient replacement for JNI
to access native libraries and memory from Java.
š¬ Still incubating ā useful for high-performance applications.
8. š¹ Strong Encapsulation of JDK Internals (JEP 403)
By default, internal JDK APIs (like sun.misc.Unsafe
) are no longer accessible via reflection.
ā Promotes better modularity and security.
9. š¹ Deprecated and Removed Features
- Applet API (deprecated)
- Experimental AOT and JIT compilers removed
- RMI Activation removed
- Security Manager deprecated
š Summary Table of Java 17 Features
Feature | Description | Type |
---|---|---|
Sealed Classes | Restrict inheritance | Final |
Pattern Matching | Cleaner instanceof
|
Final |
Records | Concise immutable classes | Final |
Switch Expressions |
switch as expression |
Final |
Text Blocks | Multi-line strings | Final |
Foreign Memory API | Native memory access | Incubator |
JDK Encapsulation | Strong module boundaries | Final |
New Rendering | Metal on macOS | Internal |
Deprecated APIs | Applets, RMI Activation | Removed |
āļø Java 17 Code Snippet Combining Major Features:
public sealed interface Shape permits Circle, Square {}
public record Circle(double radius) implements Shape {}
public record Square(double side) implements Shape {}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle(5);
String result = switch (shape) {
case Circle c -> "Area of circle: " + (Math.PI * c.radius() * c.radius());
case Square s -> "Area of square: " + (s.side() * s.side());
};
System.out.println(result);
}
}
ā Uses:
sealed interface
-
record
types switch expression
pattern matching
Ā