We will start this article with the below diagram.
In both cases (switch statement and switch expression), we use the keyword switch and terms selector expression and switch block. But there are a few important differences.
One difference is that switch
expressions cannot have empty switch blocks, unlike switch
statements. The below examples will clarify the idea.
The below code snippet is CORRECT.
String selecterExr = "A";
switch (selecterExr) {
}
But below code snippet gives compiler error: switch expression does not have any case clauses
String selecterExr = "A";
int a = switch (selecterExr) {
}
Another important difference is: a switch expression must have one or more result expressions. A switch expression evaluates to a single value and can be used in statements.
Consider the below enum type.
enum City { BENGALURU, CHENNAI, DELHI, KOLKATA, MUMBAI };
Now you want to print the occurrences of letter “A” within the given city. The code snippet below does that task. This is an example of a switch expression.
City city = City.KOLKATA;
int numOfLetterA = switch (city) {
case DELHI -> 0;
case CHENNAI, MUMBAI, BENGALURU -> 1;
case KOLKATA -> 2;
default -> throw new IllegalStateException("Invalid city: " + city);
};
System.out.println("Letter A occurs in the given city "
+ numOfLetterA + " times");
You can write the above code using switch statement as follows:
switch (city) {
case DELHI -> numOfLetterA = 0;
case CHENNAI, MUMBAI, BENGALURU -> numOfLetterA = 1;
case KOLKATA -> numOfLetterA = 2;
default -> throw new IllegalStateException("Invalid city: " + city);
};
System.out.println("Letter A occurs in the given city "
+ numOfLetterA + " times");
You can also use “case label:” in switch expression instead of “case label ->“.
City city = City.DELHI;
int numOfLetterA = switch (city) {
case DELHI:
yield 0;
case CHENNAI:
case MUMBAI:
case BENGALURU:
yield 1;
case KOLKATA:
yield 2;
default:
throw new IllegalStateException("Invalid city: " + city);
};
System.out.println("Letter A occurs in the given city "
+ numOfLetterA + " times");
You just saw two kinds of case labels, one with colon (:)and another with arrow (->). Case label along with colon and code block is called the switch block statement group. Case label along with arrow and code block is called the switch rule. Switch rule was introduced in Java 14.
With the above overview of switch statement and switch expression, we will focus on improved enum constant case labels in Java 21.
Qualified Names of Enum Constants as Case
Constants
If you look at the above code snippets, you will observe that we used selector expression of enum type (City) and used simple names of the enum’s constants (KOLKATA, BENGALURU, etc,.) for labels. Actually, this is the requirement before Java 21.
In Java 21, selector expressions don’t need to be enum type when the name of one of that enum’s constants is used as a case
constant. In Java 21, you can use qualified names of enum constants as case
constants. Have a look at the below code snippet.
interface Location {}
enum City implements Location {BENGALURU, CHENNAI, DELHI, KOLKATA, MUMBAI}
...
int numOfLetterA = 0;
Location location = City.MUMBAI;
numOfLetterA = switch (location) {
case City.DELHI:
yield 0;
case City.CHENNAI:
case City.MUMBAI:
case City.BENGALURU:
yield 1;
case City.KOLKATA:
yield 2;
default:
throw new IllegalStateException("Invalid location: "
+ location);
};
System.out.println("Letter A occurs in the given location "
+ numOfLetterA + " times");
You can write the above code snippet using switch statement as follows:
interface Location {}
enum City implements Location {BENGALURU, CHENNAI, DELHI, KOLKATA, MUMBAI}
...
int numOfLetterA = 0;
Location location = City.MUMBAI;
switch (location) {
case City.DELHI -> numOfLetterA = 0;
case City.CHENNAI, City.MUMBAI,
City.BENGALURU -> numOfLetterA = 1;
case City.KOLKATA -> numOfLetterA = 2;
default -> throw new IllegalStateException("Invalid location: "
+ location);
};
System.out.println("Letter A occurs in the given location "
+ numOfLetterA + " times");
One important point: Look at the selector expression. We are using Location as the type of selector expression. But we are passing the value of enum type City. It is possible because enum City implements the interface Location. Hence, value of type City is assignment-compatible with the value of type Location. It means a value of the City type can be assigned to a variable of the Location type.
That’s it for this article. We will discuss more on Pattern Matching for Switch in Java 21 in upcoming articles.
If you find any significant errors or want to give me some feedback, feel free to contact me at maliksanjoykumar[@]gmail.com.
Sanjoy Kumar Malik is an experienced software architect and technologist. He is passionate about Cloud Computing, Software Architecture, and System Design. Apart from technology and software, he is an avid LinkedIn networker. You can join his 5.5+ lacs supporters on LinkedIn.