Junior — Middle
Can you explain what a Data Object is in the Kotlin language and how it is used?
sobes.tech AI
Answer from AI
In Kotlin, a data class is a special class designed for storing data. The compiler automatically generates useful methods for it: equals(), hashCode(), toString(), as well as copy() and componentN() for destructuring.
It is used for simple and convenient representation of objects with a set of properties without the need to manually write boilerplate code.
Example:
data class User(val name: String, val age: Int)
fun main() {
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)
}
Data classes are widely used for data transfer, for example, in Android for models, DTOs, etc.