Java 21 - Pattern Matching for Switch - Part 1

Java 21 - Pattern Matching for Switch - Part 1

I 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.

But below code snippet gives compiler error: switch expression does not have any case clauses.

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.

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.

You can write the above code using switch statement as follows:

You can also use “case label:” in switch expression instead of “case label ->“.

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.

Note: You can use a yield statement to specify the value of a switch expression.

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.

You can write the above code snippet using switch statement as follows:

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. I will discuss more on Pattern Matching for Switch in Java 21 in my upcoming articles.

Aleksandras Mirosnicenko

Logistics Assistant @ Aldi UK | Delegating Tasks, Managing Teams

5 个月

Very informative and insightful article!!!?????? Thank you very much for your job!!!??????????????

回复

要查看或添加评论,请登录

Sanjoy Kumar Malik .的更多文章

社区洞察

其他会员也浏览了