Data Classes
Data Classes
What are Data Classes?โ
A data class is a class designed to hold data. Kotlin automatically generates:
equals()โ structural comparisonhashCode()โ hash based on propertiestoString()โ readable string representationcopy()โ create a modified copycomponentN()โ destructuring support
Basic Syntaxโ
data class Person(val name: String, val age: Int)
fun main() {
val alice = Person("Alice", 30)
println(alice) // Person(name=Alice, age=30)
}
Auto-Generated toString()โ
data class Point(val x: Int, val y: Int)
fun main() {
val p = Point(3, 4)
println(p) // Point(x=3, y=4)
}
Auto-Generated equals()โ
data class User(val name: String, val email: String)
fun main() {
val u1 = User("Alice", "alice@example.com")
val u2 = User("Alice", "alice@example.com")
val u3 = User("Bob", "bob@example.com")
println(u1 == u2) // true โ same content
println(u1 == u3) // false โ different content
println(u1 === u2) // false โ different references
}
copy() Functionโ
Create a copy with some fields changed:
data class Config(
val host: String = "localhost",
val port: Int = 8080,
val debug: Boolean = false
)
fun main() {
val dev = Config()
val prod = dev.copy(host = "example.com", debug = false)
val test = dev.copy(port = 9090, debug = true)
println(dev) // Config(host=localhost, port=8080, debug=false)
println(prod) // Config(host=example.com, port=8080, debug=false)
println(test) // Config(host=localhost, port=9090, debug=true)
}
Destructuringโ
data class Product(val name: String, val price: Double, val stock: Int)
fun main() {
val item = Product("Laptop", 999.99, 10)
val (name, price, stock) = item
println("$name costs $$price, $stock in stock")
// In loops
val products = listOf(
Product("Phone", 499.0, 25),
Product("Tablet", 299.0, 15)
)
for ((n, p, s) in products) {
println("$n โ $$p โ qty: $s")
}
}
Data Class in Collectionsโ
data class Student(val name: String, val gpa: Double)
fun main() {
val students = listOf(
Student("Alice", 3.9),
Student("Bob", 3.5),
Student("Charlie", 3.7)
)
val top = students.maxByOrNull { it.gpa }
val sorted = students.sortedByDescending { it.gpa }
println("Top student: $top")
sorted.forEach { println(it) }
}
Nested Data Classesโ
data class Address(val street: String, val city: String, val zip: String)
data class Person(
val name: String,
val age: Int,
val address: Address
)
fun main() {
val person = Person(
"Alice", 28,
Address("123 Main St", "Bangalore", "560001")
)
println(person)
println(person.address.city) // Bangalore
// Copy with nested update
val moved = person.copy(address = person.address.copy(city = "Mumbai"))
println(moved.address.city) // Mumbai
}
Data Class Requirementsโ
- Must have at least one parameter in the primary constructor.
- All primary constructor parameters must be
valorvar. - Cannot be
abstract,open,sealed, orinner.
Data Class vs Regular Classโ
// Regular class
class PointRegular(val x: Int, val y: Int)
// Data class
data class PointData(val x: Int, val y: Int)
fun main() {
val r1 = PointRegular(1, 2)
val r2 = PointRegular(1, 2)
println(r1 == r2) // false โ reference equality
val d1 = PointData(1, 2)
val d2 = PointData(1, 2)
println(d1 == d2) // true โ structural equality
}
Summaryโ
| Feature | Auto-generated by data class |
|---|---|
toString() | Yes โ shows all properties |
equals() | Yes โ structural comparison |
hashCode() | Yes โ based on properties |
copy() | Yes โ clone with overrides |
componentN() | Yes โ for destructuring |
Telemetry Integration
Completed working through this block? Sync progress to workspace.