Data classes are classes that are used for storing data. They are similar to regular classes in Kotlin, but they come with some member functions such as printing the instance to readable output, comparing instances of classes, copying them and so on which saves time on writing boilerplate
data class User(val name: String, val id: String) Examples of predefined member functions
.toString() - Prints a readable string of a class and its
properties. This is useful for debugging or logging
println(user.toString())
// or
println(user) // this way, println automatically calls .toString() for you .equals() - compares instances of a class
val user = User("Oskar", 1)
val user2 = User("Pesto", 2)
val user3 = User("Mike", 3)
println("user === user2: ${user == user2}")
println("user === user3: ${user == user3}") copy() - creates a copy of the instance
val user = User("Oskar", 1)
val user2 = User("Pesto", 2)
val user3 = User("Mike", 3)
println(user.copy("Matt"))
// print the copy of a user but with "Matt" as a name
println(user.copy(id = 3))
//print the copy of the user but with id =3