The syntax to define a data class looks simple. However, both the syntax and semantics are important. Let's get started by looking at some examples, in the following sections.
Diving into data classes
Example of syntax and semantics
Let's redefine the Emp class, which we used at the beginning of the chapter, as a data class:
record Emp(String name, int age) { } // data class - one liner
// code
The preceding code uses the record keyword to define a data class, accepting a comma-separated variable name and type, required to store the state. The compiler automatically generates default implements for the object methods (equals(), hashCode(), and toString()) for data classes...