# Can be Instantiated without the new keyword

Case classes have prebuilt companion objects with apply() implemented, so a case class can be instantiated without using new.

Why removing the new keyword? Because case classes are often used to implement algebraic data types, it’s more elegant to do so without the new keyword.

# Default equals and hashCode implementation

Case classes have default equals and hashCode implementations. Let’s pick equals and talk about it in this part, because it’s easier to verify.

Because Case classes have default equals implementation, so although person1 and person2 are different objects(I’m talking about their references), they are still equal because Scala only checks field values(name in this case) for case classes.

The result is different if we use a normal class, which compares equality by references.

# Serializable

Case classes can be serialized.

A normal class cannot be serialized by default.

# Pattern Matching

Case classes support pattern matching.

Can we achieve pattern matching using a normal class? Of course, just implement the unapply method, here is an example.

So we can use pattern matching with normal classes, but with case classes, we don’t need to write those boilerplate code any more.

# Case classes extend the Product class

Case classes extend the Product class, so it has some methods inherited from it, like productArity

# Other Interesting stuff

Case classes also have other interesting stuff, e.g. we can copy a case class by calling copy on it.

I think I’ve covered almost all the interesting parts of case classes, you can check the official Scala docs for more details.

Share