What does the "===" operator do in Kotlin?

In Kotlin, the "===" operator checks the referential equality of two objects. Any expression "a===b" will evaluate to True if and only if both "a" and "b" point to the same object. That is, both "a" and "b" share the same address.

In contrast, we can use the "==" operator to check structural equality which implies that two objects have equivalent content. Basically, "==" is a check for equals().

Example: "===" Operator in Kotlin

fun main(args: Array<String>) {
   var str1 : String = "That's it"
   var str2 : String = "It's OK"
   var str3 : String = str1

   println("str1 : " + str1)
   println("str2 : " + str2)

   // Referential Equality


It will produce the following output −

str1 : That's it
str2 : It's OK

Observe that both "str1" and "str3" have the same reference, hence "str3 === str1" returns True.

Example: "==" Operator in Kotlin (Structural Equality)

Now let's take the same example and modify it slightly to show the difference between Referential Equality and Structural Equality in Kotlin.

fun main(args: Array<String>) {
   val student1 = "Ram"
   val student2 = "shyam"
   val student3 = student1
   val student4 = "shyam"

   // Referential equality
   println("Referential Equality:")
   println(student1 === student3)

   // Structural equality
   println("Structural Equality:")
   println(student2 == student4)


It will produce the following output −

Referential Equality
Structural Equality

Here we have used the "==" operator to check the structural equality between student2 and student4. Since both the variables have the same content, "student2 == student4" returns True.