== vs ===

Kotlin language Junior Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Predict the output

What does this print?

data class Point(val x: Int, val y: Int)

fun main() {
    val a = Point(1, 2)
    val b = Point(1, 2)
    val c = a
    println("${a == b} ${a === b} ${a === c}")
}
Options
Hint 1 of 1

Which operator calls equals?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A true false true

Correct. a and b are equal but different objects; c is the same object as a.

Why not the others?
  • B true true true. a and b are two separate instances.
  • C false false true. Data classes generate equals from properties.
  • D true false false. c refers to the same instance as a.

30-second answer

== checks structural equality by calling equals (and handles null safely). === checks referential identity: whether both sides are the same object. For data classes, two objects with the same properties are == but not ===. For basic types like Int, use ==; identity on boxed numbers depends on caching and isn't meaningful.

Practise saying it · 25 s. No recording, just a timer

Explain == vs ===.

Say your answer out loud, then check which of these points you covered:

  • == calls equals
  • === compares identity
  • Use == for numbers and strings
  • Override equals with hashCode

Deep dive

a == b compiles to a?.equals(b) ?: (b === null).

Override equals and hashCode together, or use a data class.

StateFlow and Compose use equals to decide whether something changed.

Comparing floating point with == uses IEEE rules when both sides are statically typed as Float/Double (nullable too), and equals semantics otherwise, for example as Any (then NaN equals itself and -0.0 isn't equal to 0.0).

Common mistakes

  • Using === to compare strings or numbers.
  • Overriding equals without hashCode.
  • Expecting two equal data class instances to be identical.

Senior take & follow-ups

Give the data class example and say identity on numbers isn't meaningful.

Follow-up questions

Think of your answer first, then open the hint.

  1. When is === useful?

    Show hint

    Checking whether two references point to the same instance, such as caching or Compose skipping.

  2. What does == do with null?

    Show hint

    It's null-safe: null == null is true.

Compare on iOS: == vs === Swift's == and === split the same way.

Suggest a fix if something here is wrong or out of date.