What is difference between data and object class in Kotlin, and what is the purpose of each?
data class User(val name: String, val age: Int)
and
object user {
val name = ""
fun printName(name: String) = "Hello, $name!"
}
object
object
is Kotlin's way to create a singleton (one instance class) which is instantiated by the compiler.
data class
A data class is like a usual class but with a few advantages/resctrictions (Source).
Advantages
equals()
/hashCode()
toString()
componentN()
copy()
Those are created from the properties specified in the primary constructor.
Restrictions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With