What are case classes in Scala?
Answer
Case classes in Scala are special classes optimized for immutable data modeling. Declaring case class Person(name: String, age: Int) automatically provides: Immutable fields accessible as person.name. toString: Person(Alice, 30). equals and hashCode: structural equality based on fields (not reference). copy(): create a modified copy: person.copy(age = 31). Companion object with apply: create without new: val p = Person("Alice", 30). Pattern matching support: p match { case Person(n, a) => println(n) }. Serializable by default. Case classes are the idiomatic way to represent domain objects, ADT (Algebraic Data Type) variants, and message types in Akka in Scala.
Previous
What is the difference between val and var in Scala?
Next
What is pattern matching in Scala?