Kotlin Equality – Difference between “===” vs “==”

kotlin-equality-difference-referential-equality-vs-structural-equality-example-feature-image

In the tutorial, Grokonez will introduce Kotlin Equality, the difference between === and ==.

I. Kotlin Equality

We have 2 types of Kotlin equality:
– Referential equality with === operation
– Structural equality with == operation

1. Referential equality

Referential equality is used to check references with same pointing object. It represents with === operation (negative form !==).


// ###########################
// 1. referential equality
// ###########################
val str = "a"
val str1 = "a"
println(str===str1)
/*
	true
	-> a & b point to the same object
*/

val i = Integer(10)
val j = Integer(10)
println(i===j)
/*
	false
	-> i & j point to the difference objects
*/

val jack = Customer("Jack", 25)
var customer = jack;
println(jack === customer)
/*
	true
	-> Because 'jack' and 'customer' objects point to the same object 
*/	

customer = Customer("Jack", 25)
println(jack === customer)
/*
	false
	-> Because 'jack' and 'customer' objects point to the difference object 
*/

2. Structural equality

Structural equality is used to check equality of objects by equals() function with == operation (negative form !=)

a==b is translated to a?equals(b) ?: (b === null)

We can implement equals() function for object as below sample:


data class Customer(
	val name: String,
	val age: Int
){
	override final fun equals(other: Any?): Boolean{
		if(this === other) return true
		if(other === null || other !is Customer ) return false
		return (this.name == other.name && this.age == other.age)
	}
}

Practice:


val i = Integer(10)
val j = Integer(10)

println(i.equals(j))
/*
	true
*/

val jack = Customer("Jack", 25)
val customer = Customer("Jack", 25)

println(customer.equals(jack))
/*
	true
	-> because they have the same values for properties {name=Jack, age=25}
*/

3. “===” vs “==”

=== should use to compare primitives while == is recommended to check equality of objects.
=== only returns true if both references point to same objects while == can return true or false base on override function equals().

II. Full Sourcecode


package com.javasampleapproach.kotlinequality

data class Customer(
	val name: String,
	val age: Int
){
	override final fun equals(other: Any?): Boolean{
		if(this === other) return true
		if(other === null || other !is Customer ) return false
		return (this.name == other.name && this.age == other.age)
	}
}

fun main(args : Array) {
	// ###########################
	// 1. referential equality
	// ###########################
	val str = "a"
	val str1 = "a"
	println(str===str1)
	/*
 		true
 		-> a & b point to the same object
 	*/
	
	val i = Integer(10)
	val j = Integer(10)
	println(i===j)
	/*
 		false
 		-> i & j point to the difference objects
 	*/
	
	val jack = Customer("Jack", 25)
	var customer = jack;
	println(jack === customer)
	/*
 		true
 		-> Because 'jack' and 'customer' objects point to the same object 
 	*/	
	
	customer = Customer("Jack", 25)
	println(jack === customer)
	/*
 		false
 		-> Because 'jack' and 'customer' objects point to the difference object 
 	*/
	
	// 2. structural equality
	println(i.equals(j))
	/*
		true
	*/
	
	println(customer.equals(jack))
	/*
		true
		-> because they have the same values for properties {name=Jack, age=25}
	*/
}


By grokonez | January 8, 2018.

Last updated on April 17, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*