Equality in Kotlin

Dabbler :

I'm learning Kotlin, with a C++ and Java background. I was expecting the following to print true, not false. I know that == maps to equals. Does the default implementation of equals not compare each member, i.e. firstName and lastName? If so, wouldn't it see the string values as equal (since == maps to equal again)? Apparently there's something related to equality versus identity that I haven't got right in Kotlin yet.

class MyPerson(val firstName: String, val lastName: String)

fun main(args: Array<String>) {
   println(MyPerson("Charlie", "Parker") == MyPerson("Charlie", "Parker"))
}
JB Nizet :

The default equals implementation you're describing exists only for data classes. Not for regular classes where the implementation is inherited from Object, and just make the object equal to itself.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=434469&siteId=1