Junior — Middle
Can you list the key characteristics and features of data class in Kotlin?
sobes.tech AI
Answer from AI
A data class in Kotlin is a class designed to hold data with minimal boilerplate code. Key features:
equals(),hashCode(),toString(), as well ascopy()and destructuring functions are automatically generated.- Main parameters are declared in the class constructor with the
valorvarkeyword. - Used for simple data models where value semantics are important.
Example:
data class User(val name: String, val age: Int)
val user1 = User("Alice", 30)
val user2 = user1.copy(age = 31)
println(user1) // User(name=Alice, age=30)
println(user2) // User(name=Alice, age=31)