Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How does a case class differ from a normal class?

Case class differ normal
0
Posted

How does a case class differ from a normal class?

0

• You can do pattern matching on it, • You can construct instances of these classes without using the new keyword, • All constructor arguments are accessible from outside using automatically generated accessor functions, • The toString method is automatically redefined to print the name of the case class and all its arguments, • The equals method is automatically redefined to compare two instances of the same case class structurally rather than by identity. • The hashCode method is automatically redefined to use the hashCodes of constructor arguments. Most of the time you declare a class as a case class because of point 1, i.e. to be able to do pattern matching on its instances. But of course you can also do it because of one of the other points. The code example below makes use of two of the characteristics of case classes: • Instances are created without resorting to the new keyword (point 2), for example in List(MyInt(1), MyInt(2), MyInt(3)) • Constructor arguments are accessed from

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123