0% found this document useful (0 votes)
14 views3 pages

1) Details On Data Classes

This document discusses how to use the data class modifier in Kotlin to alter the default behavior of custom classes. By default, custom classes are not equal even if their properties are equal, and printing them only outputs their memory location. The data class modifier allows classes to be equal if their properties are equal, printing outputs the class name and properties, and the class supports features like destructuring and copying. The data class modifier is intended for classes that represent a bundle of data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

1) Details On Data Classes

This document discusses how to use the data class modifier in Kotlin to alter the default behavior of custom classes. By default, custom classes are not equal even if their properties are equal, and printing them only outputs their memory location. The data class modifier allows classes to be equal if their properties are equal, printing outputs the class name and properties, and the class supports features like destructuring and copying. The data class modifier is intended for classes that represent a bundle of data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

@

details on data classes


As demonstrated in the first module, basic values like String or Int can be compared or
printed. However, objects created from custom classes behave somewhat differently. Let’s
examine what happens when you compare or print these objects, and then discuss how this
behavior can be altered using a special class modifier called data.

fun main() {

println("A" == "A") // true

println("A" == "B") // false

Let's say that you have a very simple class. You can compare it to itself, but this comparison
will only return true, if exactly the same instance is on both sides of the comparison. This
means that by default, all objects created with custom classes are considered unique, they
are not equal to any other object.

class Dog(val name: String)

fun main() {

val pluto1 = Dog("Pluto")

val pluto2 = Dog("Pluto")

println(pluto1 == pluto2) // false

println(pluto1 == pluto1) // true

Printing or transforming an object into a string is also not very useful. The result should be
this class name, @ sign, and some numeric digits. The number is not really useful, it only
helps us know if two objects are the same or not.
println(pluto1) // Dog@404b9385

println("Dog: $pluto1") // Dog: Dog@404b9385

So, what can you do to get more meaningful data from printing a string? This behavior can
be altered if you use data modifier before a class. You use it before classes representing a
bundle of data. Such a class is equal to a different instance of the same class if its
constructor properties have the same value.

data class Dog(val name: String)

fun main() {

val pluto1 = Dog("Pluto")

val pluto2 = Dog("Pluto")

val rex = Dog("Rex")

println(pluto1 == pluto2) // true

println(pluto1 == pluto1) // true

println(pluto1 == rex) // false

When you transform a data class into a string, you not only have this class name, but also
values for each constructor property.

println(pluto1) // Dog(name=Pluto)

println("Dog: $pluto1") // Dog: Dog(name=Pluto)

That is not all. Data classes can be destructured, which means reading values from this
class and assigning them to variables.
data class Dog(val name: String,val age: Int)

val dog = Dog("Pluto", 7)

val (name, age) = dog

println(name) // Pluto

println(age) // 7

Beware that destructuring in Kotlin is based on position, not name, so value names need to
be placed at correct positions. For instance, if you place age at the position of name, and
name at the position of age, then you will have age in a variable called name, and name in
the variable called age.

To prevent this, always check if your variables are assigned to the correct positions of
constructor parameters.

Finally, data classes have a copy method, that creates a copy of an object. It also allows you
to specify what modifications you would like to introduce into an object.

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

fun main() {

println(dog.copy()) // Dog(name=Pluto, age=7)

println(dog.copy(age = 8)) // Dog(name=Pluto, age=8)

println(dog.copy(name = "Neptune")) // Dog(name=Neptune, age=7)

You use data modifiers for classes that are used to represent a bundle of
data. Such classes are quite common in programming.

You might also like