Difference between == and === in Kotlin
Tags: kotlin, android
Published: 2023-01-25 06:57:51

---
title: Difference between == and === in Kotlin
published: true
description: In this blog, we will learn about the difference between == and === in Kotlin.
tags: kotlin, android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6agiiq9bt0f5tyc4f4za.png
canonical_url: https://amitshekhar.me/blog/structural-and-referential-equality-in-kotlin
---

I am [**Amit Shekhar**](https://amitshekhar.me), 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](https://www.youtube.com/playlist?list=PL_I3TGB7aK6jNBMZkw3FYdJXyf7quHdI8).

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

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/structural-and-referential-equality-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:

```kotlin
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.

```kotlin
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.

```kotlin
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.

```kotlin
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.

```kotlin
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:

```kotlin
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.

```kotlin
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.

```kotlin
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.

```kotlin
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.

```kotlin
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.

```kotlin
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:

```kotlin
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](https://www.youtube.com/watch?v=lJtgxT2OIgQ)

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://amitshekhar.me/blog/kotlin-coroutines)

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)