Java Switch Expressions
July 15, 2021
Java switch
expressions are same like other expressions and evaluate to a single value. They contain labels like case L ->
and eliminate the need of break
statements to prevent fall through. To yield a value in traditional switch
statement, yield
keyword has been introduced.
Now
switch
can be used as following.
1. The
switch
can be used either statement or an expression but cannot be mixed together.
2. The
switch
expression has no fall through with following case
label form.
case label_1, label_2, ..., label_n -> expression;|throw-statement;|block
case VARANASI -> System.out.println("Varanasi");
case MADURAI, DELHI -> { int len = "Madurai".length() + "Delhi".length(); System.out.println(len); }
case 2 -> 2 * 10;
case L :
with fall through.
case BANGALORE: System.out.println("Bangalore City"); break;
switch
statement can yield a value using yield
keyword.
case BANGALORE: System.out.println("Bangalore City"); yield "Bangalore";
switch
expression was introduced in Java 12 as preview feature and previewed again in Java 13. The switch
expression has been adopted as permanent language feature in Java 14 (JEP 361).
Contents
Using Single Label
Here we will create aswitch
expression example with single label.
package com.concretepage; enum City { VARANASI, BANGALORE, MADURAI, DELHI; } public class MyApp { public static void main(String[] args) { City city = City.BANGALORE; switch (city) { case VARANASI -> System.out.println("Varanasi"); case BANGALORE -> System.out.println("Bangalore"); case MADURAI -> System.out.println("Madurai"); case DELHI -> System.out.println("Delhi"); default -> throw new IllegalStateException("Invalid city: " + city); } } }
Find one more example.
City city = City.BANGALORE; switch (city) { case VARANASI -> { int len = "Varanasi".length(); System.out.println(len); } case BANGALORE -> { int len = "Bangalore".length(); System.out.println(len); } case MADURAI -> { int len = "Madurai".length(); System.out.println(len); } case DELHI -> { int len = "Delhi".length(); System.out.println(len); } default -> { throw new IllegalStateException("Invalid city: " + city); } }
We can see that we didn't need
break
statements to prevent fall through.
Using Multiple Labels
Find theswitch
expression example with multiple labels.
City city = City.BANGALORE; switch (city) { case VARANASI, BANGALORE -> System.out.println("Varanasi".length() + "Bangalore".length()); case MADURAI, DELHI -> { int len = "Madurai".length() + "Delhi".length(); System.out.println(len); } default -> throw new IllegalStateException("Invalid city: " + city); }
Returning Value using Expression
We can return a value usingswitch
expression as following.
City city = City.BANGALORE; String data = switch (city) { case VARANASI, BANGALORE -> "Varanasi and Bangalore"; case MADURAI, DELHI -> "Madurai and Delhi"; default -> throw new IllegalStateException("Invalid city: " + city); }; System.out.println(data);
int num = 2; int output = switch (num) { case 1 -> 1 * 10; case 2 -> 2 * 10; case 3 -> 3 * 10; case 4 -> 4 * 10; default -> throw new IllegalStateException("Invalid number: " + num); }; System.out.println(output);
Using yield
In traditional switch
statement, we can return a value using yield
keyword.
City city = City.BANGALORE; String data = switch (city) { case VARANASI: case MADURAI: System.out.println("Madurai City"); yield "Madurai"; case BANGALORE: System.out.println("Bangalore City"); yield "Bangalore"; case DELHI: System.out.println("Delhi City"); yield "Delhi"; default: throw new IllegalStateException("Invalid city: " + city); }; System.out.println(data);
References
Switch ExpressionsJEP 361: Switch Expressions