What does '==' mean?

– code –

class Car(val color: String)
fun main() {
val car1 = Car(“red”)
val car2 = Car(“blue”)
val car3 = Car(“red”)
if (car1 == car2) {
println(“car1-car2: same”)
} else {
println(“car1-car2: different”)
}
if (car1 == car3) {
println(“car1-car3: same”)
} else {
println(“car1-car3: different”)
}
val x: Int = 100
val y: Int = 100
if (x == y) {
println(“x-y: same”)
} else {
println(“x-y: different”)
}
if (x === y) {
println(“x-y: same”)
} else {
println(“x-y: different”)
}
}
/*
car1-car2: different
car1-car3: different
x-y: same
x-y: same
Process finished with exit code 0
*/
– code –

I expected ‘car1-car3: same’.
I thought car1 and car3 are the same as structurally.

You should read the documentation. It is thoroughly explained there. As for your example. Add data keyword to your Car class and it will behave like you expect. Your Car is not a value-type so it is compared by reference, not by value.

Forum supports code highlight.