Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlineasy

What does a `data class` give you (and when is it a good fit)?

Tags
#data-class#dto#value-object
Back to categoryPractice quiz

Answer

It generates `equals/hashCode`, `toString`, `copy`, and `componentN` based on the primary constructor. It’s a good fit for DTO/value-like objects where equality is based on data.

Advanced answer

Deep dive

A `data class` is a convenient way to declare value-like types. Kotlin generates:

  • `equals()` / `hashCode()` based on constructor properties
  • `toString()`
  • `copy(...)`
  • `componentN()` for destructuring

When it’s a good fit

  • DTOs, request/response models, configuration objects.
  • Domain value objects where equality is “by data”.

Practical notes

  • `copy()` is shallow (it copies references).
  • Prefer `val` properties for immutability; `data class` itself doesn’t enforce it.

Common pitfalls

  • Using mutable properties in keys for HashMap/HashSet.
  • Expecting inheritance behavior (data classes are final by default).
  • Treating `copy()` as deep clone.

Related questions

Kotlin
What is destructuring in Kotlin and where do `componentN()` functions come from?
#destructuring#data-class#componentN
Kotlin
What is a data class in Kotlin and why use it?
#data-class#kotlin#dto
Java
Java `record`: what does it generate and when is it a good fit?
#java#record
#dto
Java
What is a Java record and when would you use it?
#records#dto#immutability
Architecture
DTO vs domain model — why not reuse the same class everywhere?
#dto#domain-model#api-design