Equality in Kotlin('==', '===', and 'equals'

Dmitry Klokov·2021년 1월 7일
0

KOTLIN

목록 보기
1/6
post-thumbnail
post-custom-banner

Structural Equality ('==')

== compare the data of two variables

  • Don't misunderstand this with Java ==, both are different

  • In Kotlin, == operator only compares the data or variables, whereas in Java or other languages == operator generally used to compare the references.

  • The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each otehr.

Referential equality ('===')

=== compare the reference of two object

  • It will only be true if both the objects are pointing to the same object.

  • The negated counterpart of === in Kotlin is !== which is used to compare if both the values are not equal to each other.

  • Primitive types at runtime (for example, Int), the === equality check is equivalent to the == check.

.equals method

.equals compare the content of the objects just like == operator, but it behaves differently in case of Float and Double comparison

  • equals(other: Any?) method is implemented in Any class and can be overridden in any extending class.

  • The difference between == and .equals is in case of Float and Double comparison, .equals disagress with the IEEE 754 Standard of Floating-Point Arithmetic.

What does disagree with IEEE 754 Standard for Floating-Point Arithmetic mean?

  • NaN is considered equal to itself
  • NaN is considered greater than any other element including POSITIVE_INFINITY
  • -0.0 is considered less than 0.0

Example

Compare two primitive type Int variables

val int1 = 10
val int2 = 10

println(int1 == int2)		//true
println(int1.equals(int2)	//true
println(int1 === int2)		//true

Wrapper class instead of Primitive datatype

val first = Integer(10)
val second = Integer(10)

println(first == second)		//true
println(first.equals(second))	//true
println(first === second)		//false

Class object

class Employee(val name: String)
val emp1 = Employee("Dmitry")
val emp2 = Employee("Dmitry")

println(emp1 == emp2)      //false
println(emp1.equals(emp2)) //false
println(emp1 === emp2)     //false

println(emp1.name == emp2.name)       //true
println(emp1.name.equals(emp2.name))  //true
println(emp1.name === emp2.name)      //true

The reason for the above comparison is obvious, As Empoyee is not a primitive datatype or wrapper class, all three compared the references, which returns false for all three checks. But in the case of string comparison, if only checks the contents of the string which were equal so it returns true for every case.

Wait, but you said == and .equals only compares the contents of the object which were equal in our case.

Exactly.

  • But the content comparison only works if it's a data class.
  • In case of normal class, the compiler consider both the objects as the differenct objects even if the content is same.
  • If it's a data class, the compiler compares the data and return true if the content is same.

Let's change the above class to data class.

data class Employee (val name: String)
val emp1 = Employee("Suneet")
val emp2 = Employee("Suneet")

println(emp1 == emp2)         //true
println(emp1.equals(emp2))    //true
println(emp1 === emp2)        //false

println(emp1.name == emp2.name)      //true
println(emp1.name.equals(emp2.name)) //true
println(emp1.name === emp2.name)     //true

Negative zero and Positive zero

IEEE 754 Standard for Floating-Point Arithmetic

val negZero = -0.0f
val posZero = 0.0f

println(negZero == posZero)         //true
println(negZero.equals(posZero))    //false
println(negZero === posZero)        //true

In case of Float and Double comparison, .equals disagrees with the IEEE 754 Standard for Floating-Point Arithmetic, it returns a false when -0.0 was compared with 0.0 whereas == and === returns ture.

Few things to keep in mind

  • As there is no constructor as String("") is Kotlin, all string comparison will give true if the content will be equal.
  • There's no point in optimizing code when comparing to null explicitly. a == null will be automatically translated to a === null as null is a reference and at the end, it will a reference check.
profile
Power Weekend
post-custom-banner

0개의 댓글