As the name implies, a data class is a class that stores only data and contains no operational behavior. The data classes in Kotlin save us a lot of boilerboard code (Java forces us to write a bunch of getters and setters that are “self-explanatory”) and make the resulting code easier to understand and maintain.

Creating a data class

Create a data only class for the data class using the keyword:

data class LoginUser(val username: String, val password: String)
Copy the code

Select the Tools menu bar | Kotlin | Show Kotlin the Bytecode command

Click the Decompile button in the dialog box that pops up

Decompiled Java code:

import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class LoginUser {
   @NotNull
   private final String username;
   @NotNull
   private final String password;

   @NotNull
   public final String getUsername(a) {
      return this.username;
   }

   @NotNull
   public final String getPassword(a) {
      return this.password;
   }

   // constructor
   public LoginUser(@NotNull String username, @NotNull String password) {
      Intrinsics.checkNotNullParameter(username, "username");
      Intrinsics.checkNotNullParameter(password, "password");
      super(a);this.username = username;
      this.password = password;
   }

   @NotNull
   public final String component1(a) { // component1() returns the first member username
      return this.username;
   }

   @NotNull
   public final String component2(a) { // Component2 () returns the second member, password
      return this.password;
   }

   @NotNull
   public final LoginUser copy(@NotNull String username, @NotNull String password) {
      Intrinsics.checkNotNullParameter(username, "username");
      Intrinsics.checkNotNullParameter(password, "password");
      return new LoginUser(username, password);
   }

   // $FF: synthetic method
   public static LoginUser copy$default(LoginUser var0, String var1, String var2, int var3, Object var4) {
      if ((var3 & 1) != 0) {
         var1 = var0.username;
      }

      if ((var3 & 2) != 0) {
         var2 = var0.password;
      }

      return var0.copy(var1, var2);
   }

   @NotNull
   public String toString(a) {
      return "LoginUser(username=" + this.username + ", password=" + this.password + ")";
   }

   public int hashCode(a) {
      String var10000 = this.username;
      intvar1 = (var10000 ! =null ? var10000.hashCode() : 0) * 31;
      String var10001 = this.password;
      returnvar1 + (var10001 ! =null ? var10001.hashCode() : 0);
   }

   public boolean equals(@Nullable Object var1) {
      if (this! = var1) {if (var1 instanceof LoginUser) {
            LoginUser var2 = (LoginUser)var1;
            if (Intrinsics.areEqual(this.username, var2.username) && Intrinsics.areEqual(this.password, var2.password)) {
               return true; }}return false;
      } else {
         return true; }}}Copy the code

Data classes Classes that are automatically created

The compiler automatically creates the following three functions based on the properties declared in the main constructor.

  • ToString () = LoginUser(username=” + this.username + “, password=” + this.password + “)
  • The component1() and Component2 () functions return the values of the corresponding subscripts, in the order they are declared
  • Copy () : newLoginUser(username, password) an object based on the properties of the old object. If these functions are already explicitly defined in a class or inherit from a superclass, the compiler will no longer generate them.

Syntax restrictions on data classes

Data classes have the following restrictions:

  • The main constructor takes at least one argument
  • The parameter must be identified as val or var
  • Cannot be ABSTAact, open, sealed, or inner
  • Cannot inherit from other classes (but can implement interfaces)

In addition, data classes can be used in destructuring declarations:

data class LoginUser(val username: String, val password: String)

fun main(a) {
    val loginUser = LoginUser("admin"."admin")
    val (username, password) = loginUser // select * from 'username';
    println("username = $username, password = $password")}Copy the code