Record classes
Record classes are more convenient to use in certain situations because they can be declared in a single line. The compiler automatically generates the constructor and overloads the equals()
and toString()
methods for them. We create them by writing record
before the class name.
public class Main {
public static void main(String[] args) {
Person p1 = new Person("John", 32);
Person p2 = new Person("John", 32);
System.out.println(p1);
if (p1.equals(p2)) {
System.out.println("Equal");
}
}
}
record Person(String name, int age){}