Java12 introduced syntax changes to Switch statement, and can be used as Statement or expression in java. Switch statement is used to execute based on different values and select Statements.

Switch statement in java 7 version

Java supports Integer and other primitive types in Switch case statements

package java12;
public class SwitchDemo {
    public static void main(String[] args) {

        int number = 1;
        switch(number){
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            case 4:
                System.out.println("Four");
                break;
            case 5:
                System.out.println("Five");
                break;
            default:
                System.out.println("Default");
        }

    }
}

Later It supports String types also in Switch and case expressions

package java12;
public class SwitchDemo {
    public static void main(String[] args) {

        String number = "one";
        switch(number){
            case "one":
                System.out.println("One");
                break;
            case "two":
                System.out.println("Two");
                break;
            case "three":
                System.out.println("Three");
                break;
            case "four":
                System.out.println("Four");
                break;
            case "five":
                System.out.println("Five");
                break;
            default:
                System.out.println("Default");
        }

    }
}

Java 12 introduced switch expression as a preview feature.

switch statements breaks from matched value and does not returnv value before this feature

Now, Switch allows you to return the value using expression feature

Java12 Switch Expressions

Switch expressions calculates single value and used in a case systems. Syntax

case label_1, label_2, ..., label_n -> expression;|throw-statement;|block

It contains arrow symbol(->) with left side lables, right side expressions separated by

break keyword is not required in expressions.

Switch expression alwayas assigned to an variable, Other throws an error “Not Statement”.

package java12;

public class java12expression {

        public static void main(String[] args) {

            String number = "one";
            String result = switch (number) {
                case "one" -> "one";
                case "two" -> "two";
                case "three" -> "three";
                case "four" -> "four";
                case "five" -> "five";
                default -> "Default";
            };
            System.out.println(result);
        }
    }

Multiple labels separated by comma

package java12;

public class java12expression {

        public static void main(String[] args) {

            String day = "Wednesday";
            String result = switch(day){
                case "Sunday","Saturday" ->"Week End";
                default -> "Week Day";
            };
            System.out.println(result);
        }
    }

If you need to return the value from switch expressions, use yield keyword in Java 13 version

Difference between Switch Expressions and Statements

  • Expressions return values, Statement does not return values
  • Switch Expressions should be assigned to an varaibles, Not require to assign statements
  • Expressions executes matched value case, statements executes every case and use to break an system