VOOZH about

URL: https://www.javacodegeeks.com/2020/04/java-14-looking-at-the-updated-switch-statement.html

⇱ Java 14: Looking at the updated switch statement - Java Code Geeks


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.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Michael Scharhag
Michael Scharhag
April 23rd, 2020Last Updated: April 21st, 2020
2 180 1 minute read

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

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

2 Comments
Oldest
Newest Most Voted
abramia
6 years ago

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

1
Reply
Taras Gleb
6 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!!!

0
Reply
Back to top button
Close
wpDiscuz