liveBook · Manning

Dataclass

As explained in Kotlin in Action, 2E

A data class is a Kotlin class whose compiler generates value-oriented methods such as equality, hashing, string conversion, and component functions from its primary constructor properties.

A Kotlin data class is a compact way to model values whose identity is their stored data. Marking a class with data instructs the compiler to generate standard value-oriented behavior, removing repetitive method implementations and making small domain objects easier to read and maintain. section 4.0 section 4.6

Listing 4.27 Customer as a data class
data class Customer(val name: String, val postalCode: Int)

Place the value properties in the primary constructor. This short declaration is enough to make Customer a data class. section 4.3.2

For primary-constructor properties, generated behavior includes:

  • equals, which compares the values of all primary-constructor properties rather than object identity. section 4.3.2
  • hashCode, which is based on those same properties and supports use in hash-based containers such as HashMap. section 4.3.2
  • toString, which produces a representation containing fields in declaration order. section 4.3.2
  • copy, which creates a separate instance while changing selected property values; component functions and other standard methods are also generated. section 4.6 section 4.3.2
Listing 4.28 The Customer data class has implementations for all standard methods
fun main() {
   val c1 = Customer("Sam", 11521)
   val c2 = Customer("Mart", 15500)
   val c3 = Customer("Sam", 11521)
   println(c1)
   // Customer(name=Sam, postalCode=11521)
   println(c1 == c2)
   // false
   println(c1 == c3)
   // true
   println(c1.hashCode())
   // 2580770
   println(c3.hashCode())
   // 2580770
}

Two Customer instances with identical constructor values compare equal and produce matching hash codes; printing one uses its generated string representation. section 4.3.2

Only properties declared in the primary constructor participate in equals and hashCode. A property declared elsewhere in the class is excluded, so put the fields that define value equality in the constructor. section 4.3.2

Copy instead of mutate

Use copy with a named argument when one value changes. The original instance remains unchanged, while the copy has an independent life cycle: bob.copy(postalCode = 382555). Prefer val properties when data-class instances are used as HashMap keys or shared across threads. section 4.3.2

Data classes fit naturally with Kotlin’s functional style: concise immutable data objects pair with collection operations, lambdas, and member references. Kotlin still permits mutable properties, but read-only value objects are easier to reason about and avoid invalid hash-container state after mutation. section 1.2.3 section 4.3.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains generated equality, hashing, string conversion, and copying, including primary-constructor scope and immutability rationale.
Examples
Demonstrates Person collection processing, Customer equality and hashing, and Customer.copy with a changed postal code.
What it assumes
Assumes familiarity with Kotlin classes, properties, primary constructors, and enums before deepening class features.
What you can do after
Apply it by declaring value properties in a primary constructor and using generated equality, hashing, string conversion, and copy operations.
How it connects
Connects data classes with immutable objects, HashMap, functional programming, lambda expressions, and member references.

Also explained in

Compare how 7 books explain dataclass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.