Records (introduced in Java 14)

 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

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...