Jep440

1 post in this section

Record Patterns (JEP 440): Destructuring Records with Power and Precision

Records Recap Records (Java 16, JEP 395) are transparent carriers of immutable data: record Point(int x, int y) {} record ColoredPoint(Point point, String color) {} record Line(Point start, Point end) {} The compiler generates a constructor, accessor methods (x(), y()), equals, hashCode, and toString. Before Java 21, accessing record components required calling accessor methods: Object obj = new ColoredPoint(new Point(3, 4), "red"); if (obj instanceof ColoredPoint cp) { int x = cp.

Continue reading »