Records and Text Blocks
A record is a compact class for immutable data: the compiler writes the constructor, getters, equals and toString. Text blocks use """ for multi-line strings. Both need Java 14 or newer (text blocks 15).
Example
record Point(int x, int y) {}
public class Main {
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println(p);
System.out.println(p.x() + p.y());
System.out.println(p.equals(new Point(3, 4)));
String poem = """
Roses are red,
Java is fun.
""";
System.out.print(poem);
}
}Result
Point[x=3, y=4] 7 true Roses are red, Java is fun.
Exercise
Declare an immutable data class.
Point(int x, int y) {}Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.