동등성

==

vs 자바 ==

자바는 원시타입인 경우 값이 같은지, 참조 타입인 경우 주소가 같은지 비교한다.

===

equals

equals vs ==

examples

Primitive 타입

val int1 = 10
val int2 = 10

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

wrapper class

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

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

custom class object

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

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

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

float values

val negZero = -0.0f
val posZero = 0.0f

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

Reference