1. Overview
In this article, we are going to discuss equality in Kotlin.
In Kotlin there are two types of equality:
- Structural equality (
==
) – To compare values and internally uses equals() - Referential equality (
===
) – Checks if both belongs to the same object
2. Referential equality
Referential equality is checked by using the === operator for equality and !== for inequality. a===b expression evaluates to true if both a and b point to the same object. For primitive types and String, the ===
equality check is equivalent to the == check and compares only values.
3. Structural equality
Structural equality (==) compares if both the values are the same and != to check if the values are different. In Java, we use equals() method to compare objects and == operator to compare primitive types.
Let’s see an example in Java to understand it.
public class MyClass { public static void main(String args[]) { int x=10; int y=25; System.out.println("x == y : " + (x == y)); String employeeName = "Praj"; String expectedName = "Praj"; String otherName = "Siv"; System.out.println("Employee is same as expected : " + (employeeName.equals(expectedName))); System.out.println("Employee is same as expected : " + (employeeName.equals(otherName))); } }
Result:
x == y : false
Employee is same as expected : true
Employee is same as expected : false
Since 10 and 25 are not equal, so x == y : false. employeeName and expectedName are different objects but has same values, so employeeName.equals(expectedName) returns true. employeeName and otherName are having different values, so equals method returns false.
In Kotlin, a == b is equivalent to Java’s equals method and compares values. An expression a == b translates to
a?.equals(b) ?: (b === null)
If a is not null, it calls the equals(Any?) function. It compares the values of objects and primitive types. If a is null, then (b === null) executes. Here, Kotlin referentially compares b to null as null is an object and not a value. If b is also null, then returns true.
You can check whether a variable is null using a==null. It translates to a===null referential equality and comparison happens.
fun main() { val a = 10 val b = 10 val employeeName = "Praj" val expectedName = "Praj" val otherName = "Siv" val nullVariable = null println("nullVariable : " + (nullVariable==null)) println("a equals b : " + (a==b)) println("employeeName equals expectedName : " + (employeeName == expectedName)) println("otherName equals expectedName : "+(otherName == expectedName)) }
Result:
nullVariable : true
a equals b : true
employeeName equals expectedName : true
otherName equals expectedName : false
4. Compare strings for equality
You can compare values of string variables using any of == or === operators. Both operators behave the same.
val a = "Siv" val b = "Siv" println("a equals b : " + (a == b)) println("a referencially equals b : "+(a === b))
a equals b : true
a referentially equals b : true
employeeName equals expectedName : true
employeeName reference equals expectedName : false
5. Conclusion
In this article, we have seen about equality operators in Kotlin and few examples.