dcsimg
 

The Java Switch Statement Undergoes Big Changes

by Brad Jones
The Java Switch Statement Undergoes Big Changes

The Java switch statement has some new capabilities; here's how to use them.

If you've not been paying attention to the Java language closely, then you might have overlooked some changes that have occurred to a basic language keyword. The switch statement has gotten a bit of attention and has been given some new capabilities.

Consider a piece of code using the switch statement:

    String month = "October";
    int days = 0;
    switch (month) {
        case "February":
            days = 28;
            break;
        case "April":
        case "June":
        case "September":
        case "November":
            days = 30;
            break;
        case "January":
        case "March":
        case "May":
        case "July":
        case "August":
        case "October":
        case "December":
            days = 31;
            break;
        default:
            days = 0;
            break;
    }

As you can see, this code simply takes a textual month and determines the number of days. If the value assigned to month is “October,” then days will be set to 31.  Of course, this code is the same as:

    String month = "October";
    int days = 0;
    switch (month) {
        case "February":
            days = 28;
            break;
        case "April", "June", "September", "November":
            days = 30;
            break;
        case "January", "March", "May", "July", "August",
             "October", "December":
            days = 31;
            break;
        default:
            days = 0;
            break;
    }

With the updates to the switch statement in Java, this code can be refactored to be a bit cleaner:

    String month = "October";
    int days = 0;

    switch (month) {
        case "February" -> days = 28;
        case "April", "June", "September", "November" -> days = 30;
        case "January", "March", "May", "July", "August",
             "October", "December" -> days = 31;
        default -> days = 0;
    }

The results of this code are exactly the same as the previous version. With month being equal to “October,” days will be set to 31.  Cool!

But wait! There's more!

What is even better is that switch statements are now expressions. This means the switch statement returns a value. We can refactor our code again to be even simpler:

    String month = "October";

    int days = switch (month) {
        case "February" -> 28;
        case "April", "June", "September", "November" -> 30;
        case "January", "March", "May", "July", "August",
             "October", "December" -> 31;
        default -> 0;
    };

This is a switch expression! While this was a preview in Java 12, it became standard in Java 14. As you can see, the switch is being treated as an expression that is returning value. When this code is expected, it does exactly what the previous code did - it sets days equal to 31 when month is equal to "October."

These changes make switch a much more functional construct for your Java programs.

Yield statement and more

Before leaving the topic, there are two additional things worth mentioning. First, a new yield statement was also introduced that can be used in switch expressions. The yield statement returns a value from a switch statement. That yielded value also becomes the value for the statement expression.

        String month = "October";

        int days = switch (month) {
            case "February" -> 28;
            case "April", "June", "September", "November" -> yield 30;
            case "January", "March", "May", "July", "August",
                 "October", "December" -> yield 31;
            default -> yield 0;
            };

Secondly, switch expression can be used in the old style as well as the new. The following is also valid code in Java 14:

String month = "October";

int days = switch (month) {
    case "February":
         yield 28;
    case "April":
    case "June":
    case "September":
    case "November":
         yield 30;
    case "January":
    case "March":
    case "May":
    case "July":
    case "August":
    case "October":
    case "December":
             yield 31;
    default:
              yield 0;
    };

All the code snippets presented in this article result in the same thing; The variable days is assigned the value of 31.

This article was originally published on Friday Sep 25th 2020
Home
Mobile Site | Full Site