Scala | Pattern Matching Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C. Here, "match" keyword is used instead of switch statement. "match" is always defined in Scala's root class to make its availability to the all objects. This can contain a sequence of alternatives. Each alternative will start from case keyword. Each case statement includes a pattern and one or more expression which get evaluated if the specified pattern gets matched. To separate the pattern from the expressions, arrow symbol(=>) is used. Example 1: Scala // Scala program to illustrate // the pattern matching object GeeksforGeeks { // main method def main(args: Array[String]) { // calling test method println(test(1)); } // method containing match keyword def test(x:Int): String = x match { // if value of x is 0, // this case will be executed case 0 => "Hello, Geeks!!" // if value of x is 1, // this case will be executed case 1 => "Are you learning Scala?" // if x doesnt match any sequence, // then this case will be executed case _ => "Good Luck!!" } } Output: Are you learning Scala? Explanation: In the above program, if the value of x which is passed in test method call matches any of the cases, the expression within that case is evaluated. Here we are passing 1 so case 1 will be evaluated. case_ => is the default case which will get executed if value of x is not 0 or 1. Example 2: Scala // Scala program to illustrate // the pattern matching object GeeksforGeeks { // main method def main(args: Array[String]) { // calling test method println(test("Geeks")); } // method containing match keyword def test(x:String): String = x match { // if value of x is "G1", // this case will be executed case "G1" => "GFG" // if value of x is "G2", // this case will be executed case "G2" => "Scala Tutorials" // if x doesnt match any sequence, // then this case will be executed case _ => "Default Case Executed" } } Output: Default Case Executed Important Points: Each match keyword must have at least one case clause. The last "_", is a "catch-all" case, will be executed if none of the cases matches. Cases are also called alternatives. Pattern matching does not have any break statement. Pattern matching always returns some value. Match blocks are expressions, not statements. This means that they evaluate the body of whichever case matches. This is a very important feature of functional programming. Pattern matching can also be used for value assignment and for comprehension, not only in match block. Pattern matching allows matching any sort of data with the first match policy. Each case statement returns a value, and the whole match statement is virtually a function that returns a matched value. Multiple values can be tested in a single line by using "|". Comment More infoAdvertise with us Next Article How to Check Datatype in Scala? A Astha_Singh Follow Improve Article Tags : Misc Scala Scala Practice Tags : Misc Similar Reads Using Extractors with Pattern Matching In Scala Scala Extractor is defined as an object which has a method named unapply as one of its part. Extractors can be utilized in Pattern Matching. The unapply method will be executed spontaneously, While comparing the Object of an Extractor in Pattern Matching. Below is the example of Extractor with patte 2 min read Scala Parser Combinators When a parser generator is required, some famous parsers that cross our mind are: Yacc and Bison for parsers written in C and ANTLR for parsers written in Java but they are designed to run for specific programming languages. This shortens the scope of use of parsers. However, Scala provides a unique 3 min read Interesting fact about Scala Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source 3 min read Scala Map Map is a collection of key-value pairs. In other words, it is similar to dictionary. Keys are always unique while values need not be unique. Key-value pairs can have any data type. However, data type once used for any key and value must be consistent throughout. Maps are classified into two types: m 5 min read How to Check Datatype in Scala? In this article, we will learn to check data types in Scala. Data types in Scala represent the type of values that variables can hold, aiding in type safety and program correctness. Table of Content Checking Datatype in ScalaApproach 1: Use Pattern Matching in ScalaApproach 2: Use the getClass Metho 3 min read Scala AnyRef type The Scala kind hierarchy starts with Any, that's the supertype of all types. The direct subclasses of Any are AnyVal and AnyRef. Whereas AnyVal represents price kinds (which includes Int, Double, and so on.), AnyRef represents reference types. AnyRef serves because the fundamental type and root deta 3 min read Like