Example 1: Matching on Strings

Traditional Syntax

Java




import java.io.*;
  
class GFG {
    public static void main(String args[])
    {
        // Create a string variable named 
          // "fruit" and initialize it to "banana"
        String fruit = "banana";
  
        // Use a switch statement to determine 
          // the value of the "fruit" variable
        switch (fruit) {
  
            // If the value of "fruit" is
            // "apple", execute this case
            case "apple":
                System.out.println("This is an apple.");
                break;
  
            // If the value of "fruit" is 
            // "banana", execute this case
            case "banana":
                System.out.println("This is a banana.");
                break;
  
            // If the value of "fruit" is "orange", 
            // execute this case
            case "orange":
                System.out.println("This is an orange.");
                break;
  
            // If the value of "fruit" is not "apple", 
            // "banana", or "orange", execute this case
            default:
                System.out.println("This is not an apple, banana, or orange.");
        }
    }
}


Lambda Syntax

Java




/*package whatever //do not write package name here */
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
      // Create a string variable called "fruit" and assign it the value "banana".
      String fruit = "banana";
      // Use a switch statement to check the value of the "fruit" variable.
      // Depending on the value of "fruit", print out a different message.
      switch (fruit) {
        // If the value of "fruit" is "apple", print "This is an apple."
        case "apple" -> System.out.println("This is an apple.");
        // If the value of "fruit" is "banana", print "This is a banana."
        case "banana" -> System.out.println("This is a banana.");
        // If the value of "fruit" is "orange", print "This is an orange."
        case "orange" -> System.out.println("This is an orange.");
        // If the value of "fruit" is anything else, print
        // "This is not an apple, banana, or orange."
        default -> System.out.println("This is not an apple, banana, or orange.");
      }
    }
}


Output

This is a banana.

This code checks the value of the fruit variable using a switch statement, which allows the program to take different actions depending on the value of the variable. The case statements specify the possible values of fruit, and the ‘ -> ‘ arrow notation indicates what should happen if the value matches that case. If the value of fruit does not match any of the case statements, the default case is executed.

Pattern Matching for Switch in Java

Pattern matching for a switch in Java is a powerful feature that was introduced in Java 14. Before this update, a switch expression could only be used to match the type of a value, rather than its actual value. However, by comparing the model, developers can now match the values ​​of Strings, enums, and records, which makes it much easier to write short and readable code. In traditional switch syntax, operators could only match the type of the variable, whereas, in lambda syntax, operators had to use if-else statements to match a value. Pattern matching for a switch provides an easy and efficient way to match values, making code more accurate and precise.

The syntax for pattern matching in Java is fairly straightforward and involves a ‘case’ keyword followed by an arrow (‘->’) to indicate what code should be executed if the pattern matches. In addition to matching on specific values, developers can also use pattern matching to perform more complex tasks, such as matching on specific records fields or matching on a range of values. Pattern matching for switch in Java is another exciting feature that will allow developers to write transparent and concise code, while also reducing the risk of errors. By matching real values, rather than just their types, developers can write more accurate and precise code, ultimately resulting in better software.

Similar Reads

Traditional and Lambda Syntax

In Java, a switch statement allows you to test the value of a variable and execute different codes based on the value of that variable. Both the traditional and lambda syntax can be used to write switch statements in Java....

Example 1: Matching on Strings

...

Example 2: Matching on Enum Constants

...