Difference between == and === in Kotlin

Shobhith J B
3 min readJun 13, 2023

--

Hey Guys…!. In this article I am going to explain about == and === operators in Kotlin.

Introduction

Equality operators are used to compare the equality of two variables or objects.

If the two passed objects are equal then this will return true otherwise will return false

Kotlin contains two operators for checking the equality, those are == and ===.

== operator in kotlin is called as Structural Equality Operator.
=== operator in kotlin is called as Referential Equality Operator.

What is Structural Equality Operator (==) ?

  • == Operator will check whether the values/content of two variables or objects are equal or not.
  • This operator is usually used to compare primitive data types as this will compare the values of the object/variable passed.

What is Referential Equality Operator (===) ?

  • === Operator will check whether the two variables or objects refer/points to the same memory location.
  • In simple terms, Both of the variable must point to the same object.
  • If we use === with primitive types (such as Int, Double) then the === will be converted to == i.e., it will compare the contents instead of checking for same reference.

Not understood Properly…! Don’t worry I will try to simplify the concepts with examples.

Examples…!

1. First let’s compare two primitive type variable(Int in this case) by using both == and ===.

Both of the operators will return true. Because,

== operator will compare the values/content of the variable. So, 4==4 is true.

In case of === operator, when using === with primitive types, === will be converted to == (Structural Equality Operator) operator. Thus, 4===4 will be converted to 4==4 which is true.

2. Now Let’s create our own custom Student class

Both s1 and s2 has same name(Ramesh), Then How both of the equality checks are false ? 😲

Before that you need to know what actually s1 and s2 contains. For that, let’s do println on both s1 and s2.

Here you can see both of the objects i.e., s1 and s2 has different values which means that both of them are pointing to different objects. If s1 and s2 was pointing to same location then both would have printed the same value.

Now, if we look at the previous example;

Remember the definition of == that it will compare the contents/values. But, In case of custom classes == will be converted to ===.

So for s1 === s2; both of them are different objects. Because both of them refer/point to different memory locations.

Thus, == and === also results in false.

What if we want to compare the values of two classes then? Data classes comes to rescue🙂

If we want to compare the values of two classes then we can use data class instead of normal class.

I think now it makes more sense. If we use data class it will compare the contents of class.

In the above examples === was always false. Show me a example where === is true 😑

Here s1 and s2 points to same object in the memory.

If you want to learn more about how objects are created in heap memory and how reference variable works, read the below article.

https://www.geeksforgeeks.org/reference-variable-in-java/

Thanks for reading this article. Happy Learning 🤗

Photo by Jordan Whitfield on Unsplash

--

--

Shobhith J B

Native Android Developer | Kotlin | Java | Travel Enthusiast