Equality (== structural vs === referential) in Kotlin - Performance Comparison
When checking if two things are equal in Kotlin, there are two ways: structural and referential equality.
We want to understand how the time it takes to check equality changes as the data grows.
Analyze the time complexity of the following code snippet.
val list1 = listOf(1, 2, 3, 4, 5)
val list2 = listOf(1, 2, 3, 4, 5)
// Structural equality check
val structuralEqual = list1 == list2
// Referential equality check
val referentialEqual = list1 === list2
This code compares two lists using structural equality (==) and referential equality (===).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Structural equality (==) compares each element one by one.
- How many times: It checks elements up to the size of the lists.
- Referential equality (===) just checks if both variables point to the same object once.
Structural equality checks each element, so more elements mean more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 element comparisons |
| 100 | 100 element comparisons |
| 1000 | 1000 element comparisons |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to check structural equality grows in a straight line with the size of the data.
[X] Wrong: "Checking structural equality is always as fast as referential equality."
[OK] Correct: Structural equality compares each element, so it takes longer as data grows, unlike referential equality which just checks if two variables point to the same object.
Understanding how equality checks work helps you write efficient code and explain your choices clearly in interviews.
"What if the lists contain other lists inside? How would that affect the time complexity of structural equality?"
Practice
== check when comparing two variables?Solution
Step 1: Understand the meaning of
The==in Kotlin==operator checks if two variables have the same content, meaning their values are equal.Step 2: Differentiate from
The======operator checks if two variables point to the exact same object in memory, which is different from==.Final Answer:
If the two variables have the same content (structural equality) -> Option BQuick Check:
==means structural equality [OK]
- Confusing == with ===
- Thinking == checks object identity
- Assuming == checks type equality
Solution
Step 1: Identify the operator for referential equality
In Kotlin,===checks if two variables point to the exact same object in memory.Step 2: Confirm other options
==and.equals()check structural equality, and!=checks inequality, so they don't check referential equality.Final Answer:
a === b-> Option AQuick Check:
===means referential equality [OK]
- Using == instead of === for object identity
- Confusing .equals() with ===
- Assuming != checks referential equality
val a = "hello"
val b = String(charArrayOf('h', 'e', 'l', 'l', 'o'))
println(a == b)
println(a === b)Solution
Step 1: Evaluate
Both strings contain the same characters "hello", soa == ba == bis true because it checks structural equality.Step 2: Evaluate
They are two different String objects in memory, soa === ba === bis false because it checks referential equality.Final Answer:
true\nfalse -> Option AQuick Check:
==true,===false [OK]
- Assuming === is true because contents match
- Confusing == with ===
- Thinking different objects with same content are ===
val x = listOf(1, 2, 3)
val y = listOf(1, 2, 3)
if (x === y) {
println("Same object")
} else {
println("Different objects")
}What is the output and why?
Solution
Step 1: Understand listOf creates new objects
Each call tolistOf(1, 2, 3)creates a new list object, soxandyare different objects.Step 2: Check referential equality with
Since===xandyare different objects,x === yis false, so the else branch runs.Final Answer:
Different objects -> Option CQuick Check:
===false for different list objects [OK]
- Assuming lists with same content are ===
- Confusing structural and referential equality
- Expecting compilation or runtime errors
a and b in Kotlin either have the same content or are the exact same object. Which code snippet correctly prints "Equal" if either condition is true, and "Not Equal" otherwise?Solution
Step 1: Understand the condition needed
We want to print "Equal" if eithera == b(same content) ORa === b(same object) is true.Step 2: Check each option's logic
if (a == b || a === b) { println("Equal") } else { println("Not Equal") }uses logical OR (||) correctly.if (a === b && a == b) { println("Equal") } else { println("Not Equal") }requires both to be true (AND), which is too strict.if (a == b) { println("Not Equal") } else if (a === b) { println("Equal") } else { println("Not Equal") }incorrectly prints "Not Equal" whena == bis true.if (a === b) { println("Equal") } else if (a == b) { println("Not Equal") } else { println("Equal") }prints "Not Equal" incorrectly whena == bis true buta === bis false.Final Answer:
if (a == b || a === b)-> Option DQuick Check:
Use || to combine == and === checks [OK]
- Using && instead of ||
- Confusing order of conditions
- Printing wrong messages for conditions
Ready to prove it?
Pass the test and this pattern moves unknown
