Predict the output
What does this print?
data class User(val name: String) {
var visits: Int = 0
}
fun main() {
val a = User("Ana").apply { visits = 3 }
val b = User("Ana")
println(a == b)
println(a === b)
println(a)
}Hint 1 of 1
Which properties are used by the generated functions?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A true, false, User(name=Ana)
Correct. equals and toString use only constructor properties; === compares identity.
Why not the others?
- B false, false, User(name=Ana, visits=3). visits is in the body, so it's not part of equals or toString.
- C true, true, User(name=Ana). They're two different objects.
- D true, false, User(name=Ana, visits=3). toString ignores body properties too.