Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 189

The switch Expression

Оглавление

Our second implementation of printDayOfWeek() was improved but still quite long. Notice that there was a lot of boilerplate code, along with numerous break statements. Can we do better? Yes, thanks to the new switch expressions that were officially added to Java 14.

A switch expression is a much more compact form of a switch statement, capable of returning a value. Take a look at the new syntax in Figure 3.4.

Because a switch expression is a compact form, there's a lot going on in Figure 3.4! For starters, we can now assign the result of a switch expression to a variable result. For this to work, all case and default branches must return a data type that is compatible with the assignment. The switch expression supports two types of branches: an expression and a block. Each has different syntactical rules on how it must be created. More on these topics shortly.


FIGURE 3.4 The structure of a switch expression

Like a traditional switch statement, a switch expression supports zero or many case branches and an optional default branch. Both also support the new feature that allows case values to be combined with a single case statement using commas. Unlike a traditional switch statement, though, switch expressions have special rules around when the default branch is required.

Recall from Chapter 2, “Operators,” that -> is the arrow operator. While the arrow operator is commonly used in lambda expressions, when it is used in a switch expression, the case branches are not lambdas.

We can rewrite our previous printDayOfWeek() method in a much more concise manner using case expressions:

public void printDayOfWeek(int day) { var result = switch(day) { case 0 -> "Sunday"; case 1 -> "Monday"; case 2 -> "Tuesday"; case 3 -> "Wednesday"; case 4 -> "Thursday"; case 5 -> "Friday"; case 6 -> "Saturday"; default -> "Invalid value"; }; System.out.print(result); }

Compare this code with the switch statement we wrote earlier. Both accomplish the same task, but a lot of the boilerplate code has been removed, leaving the behavior we care most about.

Notice that a semicolon is required after each switch expression. For example, the following code does not compile. How many semicolons is it missing?

var result = switch(bear) { case 30 -> "Grizzly" default -> "Panda" }

The answer is three. Each case or default expression requires a semicolon as well as the assignment itself. The following fixes the code:

var result = switch(bear) { case 30 -> "Grizzly"; default -> "Panda"; };

As shown in Figure 3.4, case statements can take multiple values, separated by commas. Let's rewrite our printSeason() method from earlier using a switch expression:

public void printSeason(int month) { switch(month) { case 1, 2, 3 -> System.out.print("Winter"); case 4, 5, 6 -> System.out.print("Spring"); case 7, 8, 9 -> System.out.print("Summer"); case 10, 11, 12 -> System.out.print("Fall"); } }

Calling printSeason(2) prints the single value Winter. This time we don't have to worry about break statements, since only one branch is executed.

Most of the time, a switch expression returns a value, although printSeason() demonstrates one in which the return type is void. Since the type is void, it can't be assigned to a variable. On the exam, you are more likely to see a switch expression that returns a value, but you should be aware that it is possible.

All of the previous rules around switch data types and case values still apply, although we have some new rules. Don't worry if these rules are new to you or you've never seen the yield keyword before; we'll be discussing them in the following sections.

1 All of the branches of a switch expression that do not throw an exception must return a consistent data type (if the switch expression returns a value).

2 If the switch expression returns a value, then every branch that isn't an expression must yield a value.

3 A default branch is required unless all cases are covered or no value is returned.

We cover the last rule shortly, but notice that our printSeason() example does not contain a default branch. Since the switch expression does not return a value and assign it to a variable, it is entirely optional.

Java 17 also supports pattern matching within switch expressions, but since this is a Preview feature, it is not in scope for the exam.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх