Junior — Middle
Have I used data class in Kotlin for modeling data?
sobes.tech AI
Answer from AI
In Kotlin, data class is used for convenient data modeling, where the main task is storing values. Such classes automatically receive equals(), hashCode(), toString(), as well as copy() functions and destructuring components.
Example:
data class User(val name: String, val age: Int)
val user = User("Ivan", 30)
println(user) // User(name=Ivan, age=30)
val olderUser = user.copy(age = 31)
This greatly simplifies working with objects that represent data, without the need to write a lot of boilerplate code.