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.

1. Traditional Syntax

The traditional syntax for a switch statement involves using the ‘switch’, ‘case’, ‘default’, and ‘break’ keywords. Here is an example:

Java




import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
      int num = 2;   // Initialize an integer variable named num with a value of 2
      String result; // Declare a String variable named result
  
      switch(num) {  // Start a switch statement using the value of num as the input
          case 1:    // If num is equal to 1, execute the code below
              result = "One"; // Set result to the String "One"
              break; // Exit the switch statement
          case 2:    // If num is equal to 2, execute the code below
              result = "Two"; // Set result to the String "Two"
              break; // Exit the switch statement
          case 3:    // If num is equal to 3, execute the code below
              result = "Three"; // Set result to the String "Three"
              break; // Exit the switch statement
          default:   // If none of the above cases match, execute the code below
              result = "Unknown"; // Set result to the String "Unknown"
              break; // Exit the switch statement
      }
      // Print the value of 
      // result to the console
      System.out.println(result);
    }
}


Output

Two

In summary, this code sets the value of the integer variable ‘num’ to 2, then uses a switch statement to check the value of ‘num’ and sets the value of the ‘result’ string variable accordingly. In this case, the value of num matches the second case (case 2:), so the result is set to the String “Two”. Finally, the value of the result is printed to the console using ‘System.out.println()’.

2. Lamba Syntax

In this example, we use a switch statement to determine the name of the day of the week based on the value of the ‘day’ variable. Each case specifies the value of the day to match, and the lambda expression after the arrow -> ‘ specifies the action to take if the value matches. The default case is executed if the value of the day is not in the range of 1-7.

Java




import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // Set the value of the day variable to 3
        int day = 3;
        // Use a switch statement to determine the name of
        // the day of the week
        switch (day) {
          case 1 -> System.out.println("Monday");   // If day equals 1, print "Monday"
          case 2 -> System.out.println("Tuesday");  // If day equals 2, print "Tuesday"
          case 3 -> System.out.println("Wednesday");// If day equals 3, print "Wednesday"
          case 4 -> System.out.println("Thursday"); // If day equals 4, print "Thursday"
          case 5 -> System.out.println("Friday");   // If day equals 5, print "Friday"
          case 6 -> System.out.println("Saturday"); // If day equals 6, print "Saturday"
          case 7 -> System.out.println("Sunday");   // If day equals 7, print "Sunday"
          default -> System.out.println("Invalid day"); // If day is not in the range 1-7, print "Invalid day"
        }
    }
}


Output 

Wednesday

Note: Lambda expressions were introduced in Java 8, so this code will only work if you are using Java 8 or later. If you are using an earlier version of Java, you will need to use the traditional syntax with colons and break statements.

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

...