Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Throughout this lesson, you’ll:

  • Learn uses for data classes in your projects.
  • Understand the requirements of using data classes.
  • Gain hands-on experience creating data classes in code.

What Are Kotlin Data Classes?

The answer to this question is simple: Kotlin data classes provide a streamlined, simple way to store and model data. For example, you can use these classes for these everyday data objects:

data class Point(val x: Int, val y: Int)

val myPoint = Point(10, 20)
val (xCoord, yCoord) = myPoint
val newPoint = myPoint.copy(x = 30)

Creating a Kotlin Data Class

With all the benefits mentioned above, you may worry that creating data classes would be challenging. Fortunately, it’s actually a straightforward process.

data class User(val name: String, val age: Int)
val user1 = User("Alice", 32)
val user1 = User("Alice", 32) 
val user2 = User("Alice", 32) 

println(user1.toString()) // User(name=Alice, age=32)
println(user2.toString()) // User(name=Alice, age=32)

val user3 = user2.copy(age = 35)

println(user3.toString()) // User(name=Alice, age=35)

Diving Deeper Into the Functions

Data classes provide a few essential functions and capabilities for free. This free functionality is one of the core reasons data classes are so powerful. Here’s the complete list of functionalities and what each piece of the data class does.

Equals and hashCode

These two functions come free with a data class. When comparing two objects, developers often need to determine if they’re equal. Knowing equality is important for concepts like sorting or determining whether the blog post the user submitted has any changes.

ToString

Data classes contain a default implementation of the toString() method. This default implementation is merely a readout of the class name, followed by each parameter and value. The nice thing about the toString() method’s default implementation is that you don’t need to write it yourself, and it’s already highly readable.

data class User(val name: String, val password: String) {
  override fun toString(): String {
    return "User(name='$name')" // No password
  }
}

Copy

Kotlin data classes provide a quick and easy way to make copies of themselves while retaining the ability to modify various properties. Every single class gets an automatically generated copy() method. If you want to make a straight copy, this method takes zero parameters. Or it can take one or more parameters of properties you wish to change in the copied data.

data class Character(val name: String, val health: Int, val inventory: List<String>)
...
val newCharacter = oldCharacter.copy(health = character.health - 25) 
// You could then put oldCharacter at the front of a state array or stack to pop off if needed.

ComponentN

Finally, Kotlin data classes implement the componentN() functions. Often, you’ll want to pull specific parameters from a data class and use those in your app. For example, you might write an app that displays the user’s name and birthdate on a dashboard. You’ll get the whole user object, but all you need to display a particular component are the name and age. Rather than passing through the entire object, you can pull out the two properties you need and only pass those.

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

val user = User("Alice", 30)
val (userName, userAge) = user
class User(val name: String, val age: Int) {
  operator fun component1(): String = name
  operator fun component2(): Int = age
}
val user = User("Alice", 30)
val (userName, userAge) = user
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo