equals() : Regular class vs Case Class

Scala Compiler automatically generates many methods including equals() method for case class
scala> // Regular Class example

scala> class RegularClass(var1: Int)
defined class RegularClass

scala> val obj1 = new RegularClass(1)
obj1: RegularClass = $iwC$$iwC$RegularClass@49ea136d

scala> val obj2 = new RegularClass(1)
obj2: RegularClass = $iwC$$iwC$RegularClass@471e8ede

scala> obj1 == obj2
res3: Boolean = false

scala>

scala> // Case Class example

scala> case class MyCaseClass(var1: Int)
defined class MyCaseClass

scala> val obj1 = new MyCaseClass(1)
obj1: MyCaseClass = MyCaseClass(1)

scala> val obj2 = new MyCaseClass(1)
obj2: MyCaseClass = MyCaseClass(1)

scala> obj1 == obj2
res4: Boolean = true

No comments:

Post a Comment