Data Classes
Data Classes
We frequently create a class to do nothing but hold data. In such a class some standard functionality is often mechanically derivable from the data. In Kotlin, this is called a data class and is marked as data
:
data class User(val name: String, val age: Int)
The compiler automatically derives the following members from all properties declared in the primary constructor:
-
equals()
/hashCode()
pair, -
toString()
of the form"User(name=John, age=42)"
, -
componentN()
functions corresponding to the properties in their order of declaration, -
copy()
function (see below).
If any of these functions is explicitly defined in the class body or inherited from the base types, it will not be generated.