0
0
Kotlinprogramming~15 mins

Equality (== structural vs === referential) in Kotlin - Hands-On Comparison

Choose your learning style9 modes available
Equality (== structural vs === referential)
📖 Scenario: Imagine you have two boxes that look the same. You want to check if the boxes have the same things inside or if they are actually the same box.
🎯 Goal: You will learn how to check if two variables in Kotlin have the same content (structural equality) or if they point to the exact same object (referential equality).
📋 What You'll Learn
Create two variables holding strings with the same text
Create a variable holding a different string
Create a variable to store the result of structural equality check
Create a variable to store the result of referential equality check
Print the results of both equality checks
💡 Why This Matters
🌍 Real World
Understanding equality helps when you want to compare data in apps, like checking if two user inputs are the same or if two objects represent the same thing.
💼 Career
Many programming jobs require knowing how to compare data correctly to avoid bugs, especially when working with objects and data structures.
Progress0 / 4 steps
1
Create two string variables with the same text
Create two variables called text1 and text2 and set both to the string "hello".
Kotlin
Need a hint?

Use val to create variables and assign the string "hello" to both.

2
Create a different string variable
Create a variable called text3 and set it to the string "world".
Kotlin
Need a hint?

Create text3 with the value "world".

3
Check structural and referential equality
Create a variable called structuralEqual that stores the result of text1 == text2 and a variable called referentialEqual that stores the result of text1 === text2.
Kotlin
Need a hint?

Use == for structural equality and === for referential equality.

4
Print the results of equality checks
Print the values of structuralEqual and referentialEqual using two separate println statements.
Kotlin
Need a hint?

Use println(structuralEqual) and println(referentialEqual) to show the results.