Skip to content
Theory · Conditions · lesson 7 of 8

The arrow switch

In a classic switch the rooms behind the doors are connected by passages, and without break execution keeps going. Since Java 14, switch has a second form, with an arrow ->. In it every door has a room of its own, with no passages to the neighbors: you walk in, run the branch, walk out.

JAVA
public class Main {
    public static void main(String[] args) {
        int day = 7;
        switch (day) {
            case 6, 7 -> System.out.println("weekend"); // weekend
            default -> System.out.println("workday");
        }
    }
}

Let's walk through the program:

  1. switch (day) evaluates day and gets 7.
  2. case 6, 7 ->: seven is in the list, and the branch prints weekend.
  3. After the branch the switch ends: no break is needed, and default is never reached.

Arrow branches

Instead of a colon, the values are followed by an arrow -> and then the branch's code. Let's see how such a branch is written.

One statement

After the arrow you write one statement, as in the example. The branch runs it and leaves the switch. There is no fall-through, so you do not write break.

A block in braces

When there are several statements, they go in braces: case 6, 7 -> { … }. After the closing brace the branch ends too, and execution leaves the switch.

Several values

The values of one branch are listed with commas: case 6, 7 ->. This replaces the labels that a classic switch stacks one after another.

Arrows and colons cannot be mixed in one switch: the compiler answers different case kinds used in the switch.

The switch expression

Most of the time a switch is there to pick a value: a day's name, a price, a message. A switch can also produce a value itself, like the ?: operator. Such a switch is called a switch expression, and it is usually written with arrows:

JAVA
int day = 7;
String kind = switch (day) {
    case 6, 7 -> "weekend";
    default -> "workday";
};
System.out.println(kind); // weekend

Let's go step by step:

  1. switch (day) picks the branch case 6, 7.
  2. The value after the arrow, "weekend", becomes the value of the whole switch.
  3. That value goes into the variable kind.
  4. The semicolon after the closing brace is required: the whole thing is one declaration, String kind = …;.

Interviewers often ask how a switch expression differs from a classic switch. They expect three points:

  • it produces a value;
  • its arrow branches do not fall through;
  • it must cover every possible value.

yield

Sometimes a branch needs to do several things before it hands over a value. Then it goes in braces, and the value is handed over with the word yield:

JAVA
int day = 3;
String kind = switch (day) {
    case 6, 7 -> "weekend";
    default -> {
        System.out.println("day " + day); // day 3
        yield "workday";
    }
};
System.out.println(kind); // workday

The default branch prints day 3 first. Then yield "workday" hands the value to the whole switch and ends the branch. A branch made of a single value needs no yield: the arrow itself hands over whatever follows it.

An answer for every case

A switch expression must produce a value for every value of the variable, or there would be nothing to put into kind. The compiler checks this in advance, before the program runs.

Here the picture with doors falls short. In a corridor you can count every door, while the int type has more than four billion values. So instead of a list, you need a default branch: a shared room for every door that is not on the list.

Common mistake

Every month is listed, but default was forgotten:

JAVA
int month = 12;
String season = switch (month) { // ✗
    case 12, 1, 2 -> "winter";
    case 3, 4, 5 -> "spring";
    case 6, 7, 8 -> "summer";
    case 9, 10, 11 -> "autumn";
};
TEXT
error: the switch expression does not cover all possible input values

All twelve months are there, but the compiler does not know that month only ever holds numbers from 1 to 12. To the compiler, month is any int, such as 13 or -5, and there is no branch for those numbers. Add the branch default -> "unknown";, and the program compiles.

Key points

  • An arrow branch does not fall through, and needs no break.
  • Several values of one branch are listed with commas: case 6, 7 ->.
  • A switch expression produces a value; a branch in braces hands it over with yield.
  • A switch expression must cover every value, so an int needs a default branch.