Tuesday, September 30, 2025

 Key Java 17 Features


1. Records

  • Introduced in Java 14 (preview), finalized in Java 16.

  • Purpose: A concise way to create immutable data classes.


Before : 

class User {
    private final String name;
    private final int age;

    User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String name() { return name; }
    public int age() { return age; }
    @Override public String toString() { return name + " (" + age + ")"; }
}


with Records : 

record User(String name, int age) {}


2) Sealed Classes
    • Introduced in Java 15 (preview), finalized in Java 17.

    • Purpose: Restrict which classes can extend or implement a type.

    • Provides more control over inheritance.

  • sealed interface Shape permits Circle, Rectangle {}


    final class Circle implements Shape {

        double radius;

    }


    final class Rectangle implements Shape {

        double length, width;

    }



    3. Enhanced Switch (Switch Expressions + Pattern Matching)

    • Modernized since Java 12 → finalized in Java 17.

    • Switch can return a value and use arrow syntax.

    Old :
    String dayType;
    switch (day) {
        case "MONDAY": case "TUESDAY": case "WEDNESDAY":
            dayType = "Weekday";
            break;
        default:
            dayType = "Weekend";
    }

    NEW : 
    String dayType = switch (day) {
        case "MONDAY", "TUESDAY", "WEDNESDAY" -> "Weekday";
        default -> "Weekend";
    };


    Also supports pattern matching (useful with sealed classes):

    static String describeShape(Shape shape) {
        return switch (shape) {
            case Circle c -> "Circle with radius " + c.radius;
            case Rectangle r -> "Rectangle " + r.length + " x " + r.width;
        };
    }


    Quick Practice Checklist

    • Convert an old POJO (like User) → record.

    • Create a sealed interface Vehicle with Car, Bike, Truck.

    • Write a switch expression that works with the Vehicle hierarchy.

    • Notice how exhaustiveness is enforced (compiler warns if you miss a case).


    4)  Pattern Matching for instanceof
    • Simplifies type checks + casting.

      if (obj instanceof String) {

          String s = (String) obj;

          System.out.println(s.length());

      }

      NOW : 

      if (obj instanceof String s) {

          System.out.println(s.length());

      }


    5) 

     Text Blocks (Multi-line Strings)

    • Introduced earlier (Java 13+), standard in Java 15+.

    • Makes multi-line strings easier (useful for JSON, SQL, XML in code).

    • String json = """

      {

        "name": "Alice",

        "age": 30

      }

      """;


    Stream.toList() and Collection Factory Methods

    • Instead of .collect(Collectors.toList()), use:

    List<String> list = Stream.of("a","b","c").toList();
    • Immutable by default.

    • Collection factory methods:

    Set<String> s = Set.of("a", "b", "c"); Map<String, Integer> m = Map.of("a",1,"b",2);

    No comments:

    Post a Comment