==
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.
===
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
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 itselfNaN
is considered greater than any other element including POSITIVE_INFINITY-0.0
is considered less than0.0
Int
variablesval int1 = 10
val int2 = 10
println(int1 == int2) //true
println(int1.equals(int2) //true
println(int1 === int2) //true
val first = Integer(10)
val second = Integer(10)
println(first == second) //true
println(first.equals(second)) //true
println(first === second) //false
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 returntrue
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
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
.
String("")
is Kotlin, all string comparison will give true
if the content will be equal.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.