I am studying Scala and ran into the following puzzle.
I can define the following case classes:
abstract class Expr
case class Number(n: Int) extends Expr
When I create two instances from the class Number and compare them
val x1 = Number(1)
val x2 = Number(1)
x1 == x2
I have the following result:
x1: Number = Number(1)
x2: Number = Number(1)
res0: Boolean = true
So x1 and x2 are the same.
However, if I drop the case modifier in the Number class definition, i.e.
abstract class Expr
class Number(n: Int) extends Expr
and then compare two instances from the Number class in the same way
val x1 = new Number(1)
val x2 = new Number(1)
x1 == x2
I have the following output:
x1: Number = Number@1175e2db
x2: Number = Number@61064425
res0: Boolean = false
It says that this time x1 and x2 are different.
Could you tell me why is this? What difference does case make in terms of comparing two instances?
Thanks, Pan