Categories

Jan 10 in JAVA

Q: Switch Expression JAVA12  feature

1 Answer

Jan 10

Java 12 has enhanced Switch expressions for Pattern matching.

Introduced in JEP 325, as a preview language feature, the new Syntax is L ->.

Following are some things to note about Switch Expressions:

The new Syntax removes the need for break statement to prevent fallthroughs.

Switch Expressions don’t fall through anymore.

Furthermore, we can define multiple constants in the same label.

default case is now compulsory in Switch Expressions.

break is used in Switch Expressions to return values from a case itself.

Classic switch statement:

String result = "";

        switch (day) {

            case "M":

            case "W":

            case "F": {

                result = "MWF";

                break;

            }

            case "T":

            case "TH":

            case "S": {

                result = "TTS";

                break;

            }

        };

        System.out.println("Old Switch Result:");

        System.out.println(result);

With the new Switch expression, we don’t need to set break everywhere thus prevent logic errors!

String result = switch (day) {

            case "M", "W", "F" -> "MWF";

            case "T", "TH", "S" -> "TTS";

            default -> {

                if(day.isEmpty())

                    break "Please insert a valid day.";

                else

                    break "Looks like a Sunday.";

            }

        };

        System.out.println(result);

Let’s run the below program containing the new Switch Expression using JDK 12.

public class SwitchExpressions {

    public static void main(String[] args)

    {

        System.out.println("New Switch Expression result:");

        executeNewSwitchExpression("M");

        executeNewSwitchExpression("TH");

        executeNewSwitchExpression("");

        executeNewSwitchExpression("SUN");

    }

    public static void executeNewSwitchExpression(String day){

        String result = switch (day) {

            case "M", "W", "F" -> "MWF";

            case "T", "TH", "S" -> "TTS";

            default -> {

                if(day.isEmpty())

                    break "Please insert a valid day.";

                else

                    break "Looks like a Sunday.";

            }

        };

        System.out.println(result);

    }

}

Since this is a preview feature, please ensure that you have selected the Language Level as Java 12 preview.

To compile the above code run the following command:

javac -Xlint:preview --enable-preview -source 12 src/main/java/SwitchExpressions.java

After running the compiled program, we get the following in the console

 

Java Switch Expressions Program Output

Switch expressions is a preview language feature. This means that even though it is complete, it may not be confirmed in the future Java Release.

Click here to read more about JAVA
Click here to read more about Insurance

Related questions

Madanswer
Apr 21 in JAVA
Jan 24 in JAVA
Jan 27 in JAVA
...