Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
class A(val x: Int)
data class B(val x: Int)
fun main() {
println(A(1) == A(1))
println(B(1) == B(1))
}false true
- Expecting
true true: thinking==compares the properties of any class. Without its ownequals, a plain class answerstrueonly for the same object. - Expecting
false false: thinking==always looks for one object. A data class generatesequals, so==compares its properties andB(1) == B(1)istrue.
- What does
B(1) === B(1)print, and why? - Which properties of a data class take part in its
equals?