What You Gain

OK, so what does that data keyword give us?

Standard Java Methods

Kotlin will automatically supply implementations of:

For Kotlin/JVM, these methods are the standard Java Object methods that ideally get overridden on most classes… yet do not, because we skip them if we think that we do not need them.

This is a subset of the Java equivalent of the Animal data class shown above:

public final class Animal {
   @NotNull
   private final String species;
   private final float ageInYears;

   @NotNull
   public final String getSpecies() {
      return this.species;
   }

   public final float getAgeInYears() {
      return this.ageInYears;
   }

   public Animal(@NotNull String species, float ageInYears) {
      this.species = species;
      this.ageInYears = ageInYears;
   }

   public String toString() {
      return "Animal(species=" + this.species + ", ageInYears=" + this.ageInYears + ")";
   }

   public int hashCode() {
      return (this.species != null ? this.species.hashCode() : 0) * 31 + Float.floatToIntBits(this.ageInYears);
   }

   public boolean equals(Object var1) {
      if (this != var1) {
         if (var1 instanceof Animal) {
            Animal var2 = (Animal)var1;

            if (this.species.equals(var2.species) && Float.compare(this.ageInYears, var2.ageInYears) == 0) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}

(we will see where this code came from later in the book, though it has been simplified here for the purposes of this chapter)

Kotlin code-generates more than this, such as the copy() function described in the next section. But, this gives you an idea of what you are getting for toString(), hashCode(), and equals().

Note, though, that Kotlin will only generate these methods for you if they are needed:

copy()

Kotlin also generates a copy() function. As the name suggests, this creates a copy of an instance of your data class. However, what the function really does is accept all of the properties as function parameters, defaulted to the current values from the instance. This allows you to selectively override values in the copy, which is a great place to use named parameters:

data class Animal(val species: String, val ageInYears: Float)

fun main() {
  val critter = Animal("frog", 3.14F)
  val youngerCritter = critter.copy(ageInYears = 0.1F)

  println(youngerCritter)
}

This results in:

Animal(species=frog, ageInYears=0.1)

So, our second Animal copied the species but has a different ageInYears, courtesy of the value that we provided to copy().

Data classes are very useful in implementing immutable objects. The copy() function then becomes a key way to create a replacement edition of an object: rather than changing the existing instance, you create a copy with all of the current information except what needs to change.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.