Difference between == and === in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Difference between == and === in Kotlin

I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

In this blog, we will learn about the difference between == and === in Kotlin.

In Kotlin, we have two types of equality:

  • Structural equality (==): It checks for equals().
  • Referential equality (===): It checks whether the two references point to the same object.

Let's learn about these two equalities in Kotlin with examples.

Suppose we have a class as below:

class Car(val color: String) {

}

Note: This is NOT a data class. So, it will not implement the equals() method by default.

First, let's first compare Structural equality.

val car1 = Car("RED")
val car2 = Car("BLUE")
println(car1 == car2)

The output:

false

It outputs false and it is obvious.

Let's take another example.

val car1 = Car("RED")
val car2 = Car("RED")
println(car1 == car2)

The output:

false

It also outputs false even though the color of both cars is the same because we have not implemented the equals() method.

Now, compare Referential equality.

val car1 = Car("RED")
val car2 = Car("RED")
println(car1 === car2)

The output:

false

It outputs false as the references to both objects are different.

Another example:

val car1 = Car("RED")
val car2 = car1
println(car1 === car2)

The output:

true

It outputs true as the references to both objects are the same now.

Till now, we have NOT implemented the equals() method, there are two ways to implement equals() method:

  1. Making the class a data class.
  2. Adding our own equals() method implementation.

Both will behave the same. So, let's go with the data class.

As our class Car was not a data class. Now, let's make our class a data class.

data class Car(val color: String) {

}

When we make it a data class, it implements equals() method internally.

Now, let's first compare Structural equality.

val car1 = Car("RED")
val car2 = Car("BLUE")
println(car1 == car2)

The output:

false

It outputs false that is obvious.

Let's take another example.

val car1 = Car("RED")
val car2 = Car("RED")
println(car1 == car2)

The output:

true

It outputs true as the color of both cars is the same and we have made the class a data class that implements the equals() method.

Now, compare Referential equality. It has no impact due to the data class.

val car1 = Car("RED")
val car2 = Car("RED")
println(car1 === car2)

The output:

false

It outputs false as the references to both objects are different.

Another example:

val car1 = Car("RED")
val car2 = car1
println(car1 === car2)

The output:

true

It outputs true as the references to both objects are the same now.

Now, we know the difference between == and === in Kotlin.

Master Kotlin Coroutines from here: Mastering Kotlin Coroutines

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.