Kotlin for Android Developers: Data Class

What is a Data Class?

A Data class in Java

A data class in Java lacks language support. It’s a repetitive and bug-prone task that represents too much friction for a modern programming language.

A data class in Kotlin

data class User(var name: String, var age: Int)
data class User(val name: String, val age: Int)

Pair and Triple

Requirements and limitations

Data classes are first-class citizens in Kotlin. In a very short syntax they offer a frictionless solution with all the advantages and no compromises.

What does it mean for Android

data class User(val name : String, val age : Int)

Addendum

data class User(val name : String, val age : Int)
data class User(val name : String, val age :Int = 0)
data class User(val name : String = "", val age : Int = 0)
data class User(val name : String = "", val age : Int)
val user = User(56) // This doesn't compile
data class User(
val name : String,
val surname : String = "",
val age : Int = 0
)
User("Steve")
User("Steve", "Jobs")
User("Steve", "Jobs", 56)
User("Steve",56) // This wont compile
data class User @JvmOverloads constructor(
val name : String,
val surname : String = "",
val age : Int = 0
)

Summary

References

Kotlin for Android Developers series

Android Developer