Java 13 Enhanced Switch
Traditional Switch
Switch statement is available in Java from the beginning. Let’s look at a normal old switch case.
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Outputs "Thursday" (day 4)
Let’s look at how it works, when a case matched and executed, the switch automatically moves to next case until and unless we provide a break. We saw an example with break and let’s look at another example without a break for one of the cases and see what would be the output.
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
//outputs Thursday and Friday
The input we are giving is 4 and when it reaches case 4, the code block of that case gets executed and as there is no break it would go for the next case and executes the code in the case 5 and it breaks the execution there as we can see there is a break statement for case 5.
Single Values in a Case
switch (day) {
case 1:
case 2:
case 3:
System.out.println("This is a weekday");
break;
}
The above switch can be written simply in a single line as
switch (day) {
case 1, 2, 3 :System.out.println("This is a weekday");
break;
}
But unfortunately this is not supported in the traditional switch case.
Multiple Values in a Case
Now, we have enhanced switch and multiple values in a case is supported.
switch (day) {
case 1, 2, 3 :System.out.println("This is a weekday");
break;
}
Switch Expression
The traditional switch that we know is a conditional statement and the new enhanced switch can also be expression just like a ternary operator which can return a value.
x = condition ? 1 : 2;
If the condition is true value of x is 1 else 2. The similar way now we can write our switch as an expression.
String msg = switch (statusCode) {
case 404:
yield "Not found!";
case 500:
yield "Internal server error!";
default:
throw new IllegalArgumentException("Unexpected value: " + statusCode);
};
The above code always return a message as there is default, if we don’t have default and if no case is matched it would throw an error stating the switch expression doesn’t cover all possible input values. And it would look like the below code.
String message = switch (statusCode) {
case 404:
yield "Not found!";
case 500:
yield "Internal server error!";
// default:
// throw new IllegalArgumentException("Unexpected value: " + statusCode);
};
Note: Switch statement uses break and never uses yield and Switch Expression the vice-versa.
Switch with Arrows
As we have seen in the above code samples, even we have multiple values in case we still have to add a break to end the statement which is not necessary if we go with arrows.
switch (statusCode) {
case 404 -> System.out.println("Not found!");
case 500 -> System.out.println("Internal server error!");
}
Note: Remember you can mix both arrows and traditional switch cases. (case: and case →).
Conclusion
- In Java 13 switch needs to be enabled explicitly
- To return a value from a switch expression you can use
break
in Java 12yield
in Java 13 - You cannot combine case: and case → in one switch
- Switch expression must cover all the possible inputs
- You can use either “:” or “→” in both switch statement and switch expression
Help me grow by dropping some suggestions. Thanks for reading and hope this article helps! Happy Coding! :)