Back to questions
JuniorTheoryCommon
What does the program print?
data class Point(val x: Int, val y: Int)
fun main() {
val p = Point(1, 2)
val q = p.copy(y = 5)
println(q)
}Point(x=1, y=5)
- Expecting
Point(x=1, y=2): thinkingcopygives back an exact copy. A named argument incopyreplaces that property in the new object. - Expecting
Point@and a hash: that is how a plain class prints. A data class generatestoString, which lists every property asname=value.
- What does
println(p)print after thecopycall? - What does
p == qprint, and what change would make ittrue?