🐦 Kotlin Beginner

What is a data class in Kotlin?

Answer

A data class is a special class designed to hold data. Kotlin automatically generates several standard methods based on the properties declared in the primary constructor: equals() and hashCode() for value comparison, toString() for readable output, copy() for creating modified copies, and componentN() functions for destructuring. Example: data class User(val name: String, val age: Int). The copy() function is particularly useful: val updated = user.copy(age = 26) creates a new User with only the age changed, leaving all other fields intact.