'===' in Kotlin

Learn via video courses
Topics Covered

The triple equals operator ('===' in Kotlin) holds a crucial role in comparison operations, yet its behavior often confuses developers. This article provides a brief overview of the === operator in Kotlin, exploring its capabilities and shedding light on its distinct functionality compared to the double equals (==) operator. We delve into scenarios where === is particularly useful, its implications for object comparisons, and how it aligns with Kotlin's focus on type safety.

'===' vs. '=='

== and === in Kotlin are two different operators used for comparison, and understanding their distinctions is crucial for writing reliable code.

== (Structural Equality Operator):

  1. Compares the values or content of two variables or objects.
  2. Frequently used for primitive data types, checking if their values are equal.
  3. In case of custom classes == will be converted to === in Kotlin.

=== (Referential Equality Operator):

  1. === in Kotlin checks whether two variables or objects refer to the same memory location.
  2. Ensures that both variables point to the exact same object in memory.
  3. When used with primitive types (e.g., Int, Double), === in Kotlin behaves like ==.

Usage of '===' in Kotlin

The operator '===' in Kotlin is primarily used for referential equality, checking if two variables or references point to the exact same object in memory. Here are common scenarios where you might use ===:

  1. Object Identity Checks
  2. Checking Null References
  3. Comparing Enum Instances

'===' with Primitives

Comparing two primitive type variables (Integers in this instance) using both the operators '==' and '===' in Kotlin.

Code:

Output:

Explanation:

  1. The '==' operator compares the values or content of the variables. Therefore, a == b evaluates to true.
  2. When using '===' in Kotlin with primitive types, it gets converted to the == operator (Structural Equality Operator).
  3. Thus, a === b is effectively equivalent to a == b, resulting in true.

'===' with Objects

Let us create our own custom class Person & check the structural & referential equality of its instances.

Code:

Output:

Explanation:

Both person1 and person2 have the same content("John", 25), then why did both of the equality checks return false ?

To investigate this, let us try printing the values of both person1 and person2.

Code:

Output:

Explanation:

Here we can notice both of the objects i.e., person1 and person2 have different values which means that both of them are pointing to different objects situated at different memory locations. If person1 and person2 were pointing to same location then both would have printed the same value.

Structural equality ('==' operator) compares the contents/values. But, in case of custom classes '==' will be converted to '===' in Kotlin.

Thus, == and === evaluate to false.

So, what do we do when we want to compare the values of two classes for their equality?

This is where the concept of data classes comes to our rescue.

Let us understand this with the help of an example.

Code:

Output:

Explanation:

  1. Since we have used data class instead of a normal class, the '==' operator compares the values or content of the variables. Therefore, person1 == person2 evaluates to true since both of them ave the same value i.e. Person("John", 25).
  2. Thus, person1 === person2 evaluates to false because both the person instances point to different memory locations.

In al the above examples, "===" in Kotlin has always evaluated to false. Let us see an example where '===' in Kotlin evaluated to true and the reason why it does so.

Code:

Output:

Explanation:

Representation on person1, person2 and person3 in Heap memory

  1. The conditions person1 == person2 and person1 == person3 evaluates to true because we have used data class Person instead of a noral class Person. '==' operator checks the contents and hence valuates to true.
  2. The condition person1 === person2 evaluates to false because person1 and person2 point to two different Person objects.
  3. The condition person1 === person3 evaluates to true because person1 and person3 point to the same Person object in the memory.

'===' and Null Safety

When using the '===' in Kotlin with nullable variables, it checks for referential equality, considering null values as well. If both variables are null or point to the same non-null object, '===' in Kotlin will return true. However, if one variable is null and the other is not, the result will be false. Let us see the following code to understand this bettter.

Code:

Output:

Explanation:

  1. The condition a===c evaluates to true because both variables reference the same non-null object.
  2. The condition a===c evaluates to false because both variables reference two different objects, one of which is null and the other being non-null.

Practical Examples

Here are some other practicalexamples on the usage of '===' in Kotlin.

Checking Singleton Objects:

We can check singletons objects using '===' in kotlin. Here's how.

Code:

Output:

Checking Referential Equality of Collections

We can check the referential equality of collections using '===' in kotlin. Here's how. Code:

Output:

Checking Refrential Equality of Enums

We can check the referential equality of enums using '===' in kotlin. Here's how. Code:

Output:

Conclusion

  1. The operator '===' in Kotlin checks for referential equality, verifying whether two variables point to the exact same object in memory.
  2. The operator '===' in Kotlin is different from '==' operator that checks the structural equality of variables i.e. the equality of their content/values.
  3. When used with primitive types, '===' in Kotlin behaves similarly to '==', comparing values.
  4. In case of custom classes '==' will be converted to '===' in Kotlin.
  5. The operator '===' in Kotlin can be used to check the referential equality of collections, enums, custom classes, singleton objects, etc. It can also be used to check null safety.