Skip to workspace
Kotlin Lab

Everyday Kotlin / Chapter 03

Compare parcel identities

Separate value equality, reference identity, and missing identifiers.

The decision

Two scanners may allocate separate String objects for the same parcel ID. The IDs match when both are present and contain the same case-sensitive text. Two missing IDs establish nothing.

Read the Kotlin

Ask what equals means for this type

Kotlin’s == performs a null-safe equality check. For two strings it compares the characters. Lists compare their elements in order. A regular class that inherits Object’s equality uses object identity until it overrides equals. In Java the closest spelling for two nullable references is Objects.equals(a, b).

val a = listOf("red", "blue")
val b = mutableListOf("red", "blue")
a == b  // true

Decide what two missing IDs mean

Write down the missing/missing case separately. Kotlin considers two nulls equal. In this domain a parcel without an ID cannot identify any parcel, even another one without an ID. A non-null guard encodes that extra rule. With one missing ID and one present ID, ordinary equality already returns false.

fun matches(a: String?, b: String?) = a != null && a == b

Follow aliases when identity matters

Use === to ask whether two references designate the very same object. Assigning a list to a second variable makes an alias. Copying its elements creates a separate list that can start out equal. Mutating the original is a quick way to reveal which variable is an alias.

val basket = mutableListOf("tea")
val alias = basket
val copy = basket.toMutableList()
basket.add("milk")  // alias sees milk, copy still has tea

The full example

Java 25
import java.util.Objects;

class Lesson {
    static boolean sameParcel(String left, String right) {
        return left != null && Objects.equals(left, right);
    }
}
Kotlin/JVM
fun sameParcel(left: String?, right: String?): Boolean =
    left != null && left == right

Inputs and outcomes

InputOutcomeWhy it matters
Two separately allocated IDs containing PK7trueObject identity is irrelevant to the scan.
null and nullfalseA missing ID cannot prove the parcels agree.
PK7 and pk7falseCase is part of the ID.

Walk through the code

  1. Java uses Objects.equals to compare nullable references by their equals contract. Kotlin spells that comparison ==.
  2. Both equality operations consider two nulls equal. The left != null guard rules out that case, and the comparison already returns false for a missing right operand.
  3. On the JVM, String implements content equality. Kotlin delegates to equals for reference values, so an ordinary custom class inherits identity behavior unless it supplies a different implementation.

The Java habit

Java’s == between String references tests identity. Translating its spelling directly changes the comparison. Kotlin has === when the two operands must be the same object.

When to choose differently

Content equality is the wrong test when tracking whether two variables point to the same mutable buffer. Distinct buffers can hold equal data.

References

Open quizTry code