Core Java

Java 14: Looking at the updated switch statement

JDK 14, released in March 2020, comes with an updated version of the switch statement. This has been a preview feature in JDK 12 and JDK 13.

To see the difference, let’s look at a simple example. Assume we want to compute the daily working time based on a DayOfWeek enum.

With the old way of using the switch statement, our solution might look like this:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
DayOfWeek day = ...
float expectedWorkingTime;
 
switch (day) {
    case MONDAY:
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
        expectedWorkingTime = 8f;
        break;
    case FRIDAY:
        expectedWorkingTime = 6f;
        break;
    default:
        expectedWorkingTime = 0f;
}

With the new switch statement (or expression) we can rewrite our example like this:

1
2
3
4
5
6
7
DayOfWeek day = ...
 
final float expectedWorkingTime = switch (day) {
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> 8f;
    case FRIDAY -> 6f;
    default -> 0f;
};

So, what’s new:

  • The switch keyword can be used as expression and return a value. In this example the value returned by switch is assigned to expectedWorkingTime. Note that this allows us to make expectedWorkingTime final which was not possible in the previous solution.
  • A case statement can contain multiple values, separated by comma.
  • In the case statement, colon is replaced with an arrow (->)
  • When using the arrow (->) syntax, no break keyword is required. If you prefer using break, you can still use the older colon syntax for cases.

The new yield statement

In the previous example we return a simple value on the right side of the arrow (->). However, maybe we need to compute this value first, for which we might need a few extra lines of code.

For example:

01
02
03
04
05
06
07
08
09
10
final float expectedWorkingTime = switch (day) {
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> {
        if (isFullTimeEmployee) {
            yield 8;
        }
        yield 4;
    }
    case FRIDAY -> 6f;
    default -> 0f;
};

Here we use a code block in the first case statement to determine the working time. With the new yield statement we return a value from a case block (like using return in methods).

You can find the examples shown in this post on GitHub.

Published on Java Code Geeks with permission by Michael Scharhag, partner at our JCG program. See the original article here: Java 14: Looking at the updated switch statement

Opinions expressed by Java Code Geeks contributors are their own.

Michael Scharhag

Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
abramia
abramia
3 years ago

Thank you. Very simple, succinct and clear explanation of how to use the new switch expression.

Taras Gleb
Taras Gleb
3 years ago

Great post Michael,thanks a thousand :) . A quick note to language developers and the community, from the 25 year java veteran..if the only thing ‘yield’ does is RETURNS the value, why introduce the ambiguity and call it yield and not just return.
Keep up the great job Michael!!!

Back to top button