Aryan PrajapatKnowledge Contributor
What is a data class in Kotlin?
What is a data class in Kotlin?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
In Kotlin, a data class is a special type of class that is primarily used to hold data/state rather than behavior. It is designed to automatically generate common methods such as equals(), hashCode(), toString(), and copy() based on the properties defined in the class.
data class Person(val name: String, val age: Int)
val person = Person(“John”, 25)
println(person) // Output: Person(name=John, age=25)
In the example, the Person class is defined as a data class with properties name and age. The toString() method is automatically generated and displays the property values when the person instance is printed. Data classes are useful for modeling data-centric structures and automatically providing useful methods for working with the data.