Deep Kotlin tutorial covering ==, ===, data class copy, shallow copy with examples, diagram, mistakes, and interview answers.

Equality, Copying, and Shallow Copy Problems

What is Equality, Copying, and Shallow Copy Problems?

Equality, Copying, and Shallow Copy Problems is an important Kotlin topic from 1.10 Common Kotlin Pitfalls in Android. In this lesson we focus only on the concepts that belong together: ==, ===, data class copy, shallow copy, mutable nested data. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

Why students should learn it

  • Understand Equality, Copying, and Shallow Copy Problems in simple language
  • Use == in real Kotlin or Android code
  • Explain Equality, Copying, and Shallow Copy Problems in interviews with examples and trade-offs

How it works

Equality, Copying, and Shallow Copy Problems: ==, ===, data class copy, shallow copy, mutable nested data works by separating two different questions: whether two references point to the same object (===) and whether two objects hold equal data (==, which data class already implements for you). copy() on a data class only copies the top level, so nested mutable objects are still shared unless you copy them too.

  1. Identify what problem Equality, Copying, and Shallow Copy Problems: ==, ===, data class copy, shallow copy, mutable nested data solves.
  2. Write the smallest Kotlin snippet that demonstrates it.
  3. Add one Android or JVM use case where it matters.
  4. Compare it with a similar concept so the difference is clear.
  5. Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.

Types / Classification

  • ==: core concept in this lesson
  • ===: core concept in this lesson
  • data class copy: core concept in this lesson
  • shallow copy: core concept in this lesson
  • mutable nested data: core concept in this lesson
  • Structural equality (==): compares data/content
  • Referential equality (===): compares identity
  • Shallow copy: copies only the top-level fields

Important differences

Concept 1Concept 2How to remember it
Shallow copyDeep copyShallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.
======= checks structural equality (same data); === checks referential equality (same object in memory).
copy()clone()-style deep copycopy() is generated for data classes and is shallow by default; a true deep copy needs manual handling of any mutable nested fields.

Diagram

flowchart TD
A["1.10 Common Kotlin Pitfalls in Android"] --> B["Equality, Copying, and Shallow Copy Problems"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]

Examples

== vs === on data classes

data class User(val name: String)
val a = User("Asha")
val b = User("Asha")
a == b   // true, same data
a === b  // false, different objects

Shallow copy keeps nested objects shared

data class Cart(val items: MutableList<String>)
val original = Cart(mutableListOf("Book"))
val copy = original.copy()
copy.items.add("Pen")
// original.items also has "Pen" - same list instance

When to use it

Use it when: Use this to model real domain concepts clearly - a data class for a User or Order, a sealed class for a finite set of UI states, an interface for a contract that several classes share.

Avoid it when: Avoid deep inheritance hierarchies for code reuse alone; prefer composition when the relationship is not a genuine is-a relationship.

Common mistakes

  • Assuming copy() on a data class produces a fully independent deep copy
  • Using == when the intent was to check object identity, or === when data equality was intended
  • Mutating a nested list/map shared between an original object and its copy
  • Relying on default equals()/hashCode() after manually overriding one but not the other
  • Forgetting that arrays use reference equality with == unlike List

Interview questions

Read each answer in four parts: simple answer, code example, trade-off, and common mistake.

1. What is Equality, Copying, and Shallow Copy Problems in 1.10 Common Kotlin Pitfalls in Android?

Answer: Equality, Copying, and Shallow Copy Problems is an important Kotlin topic from 1.10 Common Kotlin Pitfalls in Android. In this lesson we focus only on the concepts that belong together: ==, ===, data class copy, shallow copy, mutable nested data. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.

data class User(val name: String)
val a = User("Asha")
val b = User("Asha")
a == b   // true, same data
a === b  // false, different objects
  • Trade-off: Shallow copy and Deep copy solve related but different problems: Shallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.
  • Common mistake: Assuming copy() on a data class produces a fully independent deep copy

2. How does Equality, Copying, and Shallow Copy Problems work in Kotlin or Android code?

Answer: Equality, Copying, and Shallow Copy Problems: ==, ===, data class copy, shallow copy, mutable nested data works by separating two different questions: whether two references point to the same object (===) and whether two objects hold equal data (==, which data class already implements for you). copy() on a data class only copies the top level, so nested mutable objects are still shared unless you copy them too.

data class Cart(val items: MutableList<String>)
val original = Cart(mutableListOf("Book"))
val copy = original.copy()
copy.items.add("Pen")
// original.items also has "Pen" - same list instance
  • Trade-off: Shallow copy and Deep copy solve related but different problems: Shallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.
  • Common mistake: Using == when the intent was to check object identity, or === when data equality was intended

3. What is one common mistake with Equality, Copying, and Shallow Copy Problems?

Answer: A common mistake is: Assuming copy() on a data class produces a fully independent deep copy Another is: Using == when the intent was to check object identity, or === when data equality was intended

data class User(val name: String)
val a = User("Asha")
val b = User("Asha")
a == b   // true, same data
a === b  // false, different objects
  • Trade-off: Shallow copy and Deep copy solve related but different problems: Shallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.
  • Common mistake: Assuming copy() on a data class produces a fully independent deep copy

4. How would you explain Equality, Copying, and Shallow Copy Problems with a small example?

Answer: Equality, Copying, and Shallow Copy Problems is an important Kotlin topic from 1.10 Common Kotlin Pitfalls in Android. In this lesson we focus only on the concepts that belong together: ==, ===, data class copy, shallow copy, mutable nested data. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.

data class User(val name: String)
val a = User("Asha")
val b = User("Asha")
a == b   // true, same data
a === b  // false, different objects
  • Trade-off: Shallow copy and Deep copy solve related but different problems: Shallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.
  • Common mistake: Using == when the intent was to check object identity, or === when data equality was intended

5. What is the trade-off of using Equality, Copying, and Shallow Copy Problems?

Answer: Shallow copy and Deep copy solve related but different problems: Shallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.

data class Cart(val items: MutableList<String>)
val original = Cart(mutableListOf("Book"))
val copy = original.copy()
copy.items.add("Pen")
// original.items also has "Pen" - same list instance
  • Trade-off: Shallow copy and Deep copy solve related but different problems: Shallow copy duplicates only the top-level object; nested mutable objects are still shared. Deep copy also duplicates nested objects.
  • Common mistake: Assuming copy() on a data class produces a fully independent deep copy

Practice task

Write one Kotlin example for Equality, Copying, and Shallow Copy Problems. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.