Thursday 7 December 2017

Kotlin: === vs ==

Kotlin supports two types of equality.
a.   Referential Equality
b.   Structural Equality

Referential Equality (===)
 ‘===’ operator is used to check, whether two references point to same object or not.

Ex:
ref1 === ref2

Above statement returns true, if references ‘ref1’ and ‘ref2’ points to same object, else false.

Structural Equality(==)
Structural equality is used to check, whether the values of both references are equal (checks by using equals() method of java).

Ex:
ref1 == ref2

Above statement returns true, if ref1.equals(ref2) return true, else false.

Equality.kt

fun main(args: Array<String>) {
 var intVar1 : Int = 10000
 var intBoundedVar1 : Int? = intVar1
 var intBounderdVar2 : Int? = intVar1
 
 println("(intBoundedVar1===intBounderdVar2) : ${(intBoundedVar1 === intBounderdVar2)}")
 println("(intBoundedVar1==intBounderdVar2) : ${(intBoundedVar1 == intBounderdVar2)}")
}

Output

(intBoundedVar1===intBounderdVar2) : false
(intBoundedVar1==intBounderdVar2) : true


Previous                                                 Next                                                 Home

No comments:

Post a Comment