0
0
Kotlinprogramming~10 mins

String comparison (equals, compareTo) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String comparison (equals, compareTo)
Start
Get two strings
Use equals()
Are strings exactly same?
YesReturn true
Return false
Use compareTo()
Compare lex order
Return negative/zero/positive
End
First, we check if two strings are exactly equal using equals(). Then, we compare their order lexicographically using compareTo(), which returns a number indicating their order.
Execution Sample
Kotlin
val a = "apple"
val b = "Apple"
println(a.equals(b))
println(a.compareTo(b))
This code compares two strings 'apple' and 'Apple' using equals() and compareTo(), printing the results.
Execution Table
StepActionEvaluationResult
1Assign a = "apple"a = "apple"a = "apple"
2Assign b = "Apple"b = "Apple"b = "Apple"
3Evaluate a.equals(b)"apple".equals("Apple")false
4Evaluate a.compareTo(b)"apple".compareTo("Apple")32
5Print equals resultfalseOutput: false
6Print compareTo result32Output: 32
💡 All steps completed, program ends after printing results.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
aundefined"apple""apple""apple"
bundefinedundefined"Apple""Apple"
Key Moments - 2 Insights
Why does equals() return false even though the words look similar?
equals() checks exact match including case. "apple" and "Apple" differ in uppercase 'A', so it returns false (see step 3 in execution_table).
What does the number 32 from compareTo() mean?
compareTo() returns a positive number because lowercase 'a' has a higher Unicode value than uppercase 'A'. The number 32 is the difference in their Unicode codes (see step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of a.equals(b) at step 3?
Atrue
Bfalse
Cnull
D32
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the program print the compareTo() result?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look for the step where output shows 'Output: 32' in execution_table.
If we change b to "apple", what would a.equals(b) return?
Atrue
B32
Cfalse
Dnull
💡 Hint
Refer to how equals() works in key_moments and execution_table step 3.
Concept Snapshot
String comparison in Kotlin:
- Use equals() to check exact match (case-sensitive).
- Use compareTo() to get lexicographic order:
  returns 0 if equal,
  negative if first < second,
  positive if first > second.
- equals() returns Boolean, compareTo() returns Int.
Full Transcript
This example shows how Kotlin compares strings. First, two strings a and b are assigned values "apple" and "Apple". The equals() function checks if they are exactly the same, including case, and returns false because 'a' and 'A' differ. Then compareTo() compares their lexicographic order and returns 32, a positive number, because lowercase 'a' has a higher Unicode value than uppercase 'A'. The program prints false and 32 as results. This teaches that equals() is strict and case-sensitive, while compareTo() tells order by returning a number.