• Articles
  • Tutorials
  • Interview Questions

Scala Pattern Matching and Case Class - A Comprehensive Guide

Scala Pattern Matching

It is a generalization of C or Java’s switch statement. This matching method is used instead of a switch statement. It is defined in Scala’s root class Any and therefore is available for all objects. The match method takes a number of cases as an argument. Each alternative takes a pattern and one or more expressions that will be performed if the pattern matches. A symbol => is used to separate the pattern from the expressions.

Enroll yourself in Online Scala and Apache Spark training and give a head-start to your career in Scala!

Watch this Apache-Spark-Scala video

Refer to our blog for detailed information about Scala Collection.


object Intellipaat {
def main(args: Array[String]) {
def matchValue(i: Int): String = i match {
case 1 => "one"
case 2 => "two"
case 3=> "three"
case _=> "unknown"

If patterns are of a different type in different cases use Any except Int and String in the above example.

Check out Scala certification blog!

Case Classes in Scala

These are the special types of classes that are used for pattern matching with case expressions. By adding a case keyword there is the number of advantages which are:

  • The compiler automatically changes the constructor arguments into immutable fields.
  • The compiler automatically includes equals, hashCode and toString methods to the class

Still, have queries? Come to Intellipaat’s Big Data Community, clarify all your doubts, and excel in your career!

Certification in Bigdata Analytics


case class Calculator(Value: Type)

Check out the top Apache Spark and Scala Interview Questions to learn what is expected from Apache Spark and Scala professionals!


object Intellipaat {
def main(args: Array[String]) {
val a = new employee(1, “abc”)
val b  = new employee(2, “xyz”)
val c  = new employee(3, “pgr”)
for (employee <- List(a, b, c)) {
employee match {
case employee(1, “abc”) => println("Hello abc")
case employee(2, “xyz”) => println(“Hello xyz”)
case employee(id, employee_name) => println("ID: " +id + ", Employee:" + employee_name)
case class employee(id: Int, employee_name: String) // case class

Hello abc
Hello xyz
ID: 3, Employee: pqr

Interested in learning Scala? Click here to learn more in this Scala Training in Toronto!

Course Schedule

Name Date Details
Big Data Course 22 Jun 2024(Sat-Sun) Weekend Batch
View Details
Big Data Course 29 Jun 2024(Sat-Sun) Weekend Batch
View Details
Big Data Course 06 Jul 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Technical Reseach Analyst - Data Engineering

Abhijit is a Technical Research Analyst specializing in Deep Learning. He holds a degree in Computer Science with a focus on Data Science. Being proficient in Python, Scala, C++, Dart, and R, he is passionate about new-age technologies. Abhijit crafts insightful analyses and impactful content, bridging the gap between cutting-edge research and practical applications.