Introduction
Data classes in Kotlin are a special kind of class designed to hold data. They provide a concise syntax for defining classes whose primary purpose is to store values. Data classes automatically provide methods like equals()
, hashCode()
, toString()
, copy()
, and component functions. This makes them very convenient for working with immutable data.
Defining a Data Class
To define a data class, use the data
keyword before the class
keyword. A data class must have at least one primary constructor parameter.
Syntax
data class ClassName(val property1: Type, val property2: Type)
Example
fun main() {
val person = Person("Rahul", 25)
println(person)
val anotherPerson = person.copy(name = "Priya")
println(anotherPerson)
val (name, age) = person
println("Name: $name, Age: $age")
}
data class Person(val name: String, val age: Int)
Explanation:
data class Person(val name: String, val age: Int)
: Defines a data classPerson
with propertiesname
andage
.println(person)
: Prints the string representation of theperson
object.val anotherPerson = person.copy(name = "Priya")
: Creates a copy of theperson
object with a modifiedname
property.val (name, age) = person
: Destructures theperson
object into its properties.
Output:
Person(name=Rahul, age=25)
Person(name=Priya, age=25)
Name: Rahul, Age: 25
Properties of Data Classes
equals()
and hashCode()
Data classes automatically generate equals()
and hashCode()
methods based on the properties defined in the primary constructor.
Example
fun main() {
val person1 = Person("Rahul", 25)
val person2 = Person("Rahul", 25)
val person3 = Person("Priya", 30)
println(person1 == person2) // True, because the properties are the same
println(person1 == person3) // False, because the properties are different
}
data class Person(val name: String, val age: Int)
Output:
true
false
toString()
The toString()
method provides a string representation of the data class instance, including the class name and its properties.
Example
fun main() {
val person = Person("Rahul", 25)
println(person.toString())
}
data class Person(val name: String, val age: Int)
Output:
Person(name=Rahul, age=25)
copy()
The copy()
method creates a new instance of the data class with the same properties, allowing optional modifications to some properties.
Example
fun main() {
val person = Person("Rahul", 25)
val anotherPerson = person.copy(name = "Priya")
println(anotherPerson)
}
data class Person(val name: String, val age: Int)
Output:
Person(name=Priya, age=25)
Component Functions
Data classes automatically generate component functions for each property, which can be used for destructuring declarations.
Example
fun main() {
val person = Person("Rahul", 25)
val (name, age) = person
println("Name: $name, Age: $age")
}
data class Person(val name: String, val age: Int)
Output:
Name: Rahul, Age: 25
Example Program with Data Classes
Here is an example program that demonstrates various aspects of data classes in Kotlin:
fun main() {
// Creating an instance of the data class
val student = Student("Rahul", 21, "Computer Science")
println(student)
// Copying the data class instance
val anotherStudent = student.copy(name = "Priya")
println(anotherStudent)
// Destructuring declarations
val (name, age, major) = student
println("Name: $name, Age: $age, Major: $major")
// Comparing data class instances
val student1 = Student("Rahul", 21, "Computer Science")
val student2 = Student("Rahul", 21, "Computer Science")
val student3 = Student("Amit", 22, "Mathematics")
println(student1 == student2) // True
println(student1 == student3) // False
// Using toString method
println(student1.toString())
}
data class Student(val name: String, val age: Int, val major: String)
Output:
Student(name=Rahul, age=21, major=Computer Science)
Student(name=Priya, age=21, major=Computer Science)
Name: Rahul, Age: 21, Major: Computer Science
true
false
Student(name=Rahul, age=21, major=Computer Science)
Conclusion
In this chapter, you learned about data classes in Kotlin, including how to define data classes, use properties, and leverage automatically generated methods such as equals()
, hashCode()
, toString()
, copy()
, and component functions. Data classes are a powerful feature in Kotlin that simplify the creation of classes intended to hold data, making your code more concise and readable.