Records (introduced in Java 14)
Records are a compact way to declare classes that are primarily used to store data. They provide automatic implementations of equals(), hashCode(), toString(), and more. Here's an example:
record Person(String name, int age) {
// Implicitly generated constructors, accessors, equals, hashCode, toString
}
Person person = new Person("John Doe", 30);
System.out.println(person.name()); // John Doe
System.out.println(person.age()); // 30
No comments:
Post a Comment