๐ŸŽ“ A1Learner
๐Ÿ”
0 of 30 lessons completed

Java Record Patterns

A record pattern pulls the components out of a record in one match. A nested record pattern reaches into a record held inside another record.

A Point pattern binds both numbers

The pattern binds x and y from the Point. Their sum prints.

public class Main {
  static String describe(Object value) {
    return switch (value) {
      case Point(int x, int y) -> "Point sum: " + (x + y);
      default -> "Not a point";
    };
  }
  public static void main(String[] args) {
    System.out.println(describe(new Point(3, 4)));
    System.out.println(describe("hello"));
  }
}
record Point(int x, int y) {
}

A Box pattern reaches the Point inside

The nested pattern binds the numbers inside the Box. The plain Point pattern sits below it.

public class Main {
  static String describe(Object value) {
    return switch (value) {
      case Box(Point(int x, int y)) -> "Box sum: " + (x + y);
      case Point(int x, int y) -> "Point sum: " + (x + y);
      default -> "Other";
    };
  }
  public static void main(String[] args) {
    System.out.println(describe(new Box(new Point(2, 5))));
    System.out.println(describe(new Point(1, 1)));
  }
}
record Point(int x, int y) {
}
record Box(Point point) {
}

A record pattern inside instanceof

The instanceof test binds x and y in one step. The sum prints inside the branch.

public class Main {
  public static void main(String[] args) {
    Object value = new Point(6, 1);
    if (value instanceof Point(int x, int y)) {
      System.out.println("Point sum: " + (x + y));
    } else {
      System.out.println("Not a point");
    }
  }
}
record Point(int x, int y) {
}

Example

import java.util.List;
public class Main {
  static String describe(Object value) {
    return switch (value) {
      case Box(Point(int x, int y)) -> "Box sum: " + (x + y);
      case Point(int x, int y) -> "Point sum: " + (x + y);
      default -> "Other";
    };
  }
  public static void main(String[] args) {
    List<Object> values = List.of(new Box(new Point(4, 5)), new Point(3, 3), "hello");
    for (Object value : values) {
      System.out.println(describe(value));
    }
  }
}
record Point(int x, int y) {
}
record Box(Point point) {
}

Key points

  • A record pattern names the record and a pattern for each component.
  • The pattern matches only when the record and every component match.
  • A nested pattern pulls the numbers out of a record inside a record in one step.
  • A var pattern matches any component value and binds it to a variable.
  • A record pattern inside instanceof binds the components for the code that follows.

In this example, the switch pulls the nested numbers out of the Box, binds the plain Point below it, and sends the String to the default branch.

Note

A record pattern fails as a whole when any part of it fails.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does case Box(Point(int x, int y)) bind for a Box holding Point(2, 5)?