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

Ā 

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.