Search⌘ K
AI Features

Referential vs Structural Equality

Explore the differences between referential and structural equality in Kotlin. Understand how to use === for referential checks and == for structural equality, and learn the properties that equals implementations must follow. This lesson prepares you to write precise equality checks and avoid common pitfalls when comparing objects in Kotlin.

Definition

In any programming language, we have two ways to compare objects and check if they are equal:

  • referential equality,
  • structural equality.

Referential equality of two objects means that they point to the exact same object in memory, i.e. they use the same reference.

Structural equality on the other hand means that two objects have the same value. They may still point to different places in memory though, i.e. they don’t necessarily hold the same reference.

Note: ...