Java 16: Records for concise data carriers

A record auto-generates constructor, accessors, equals, hashCode and toString.

Code
record Point(int x, int y) {}
Point p = new Point(3, 4);
System.out.println(p);
System.out.println("x=" + p.x() + " y=" + p.y());
System.out.println(p.equals(new Point(3, 4)));
Output
Point[x=3, y=4]
x=3 y=4
true
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26