Difference between == and === in Kotlin

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

I am Amit Shekhar, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join my program and get high paying tech job: amitshekhar.me

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.

Let’s take 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, no need of checking for the equals() method implementation.

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.

Let’s take 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, no need of checking for the equals() method implementation although it is a data class. The output will be the same even if it was not a data class as we have seen earlier.

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.

Watch the video format: Difference between == and === in Kotlin

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.