Skip to content
Justice Tech School

switch Expressions

The arrow form returns a value, needs no break and can group cases.

Example
public class Main {
  public static void main(String[] args) {
    int day = 3;
    String name = switch (day) {
      case 1, 7 -> "Weekend";
      case 2, 3, 4, 5, 6 -> "Weekday";
      default -> "?";
    };
    System.out.println(name);
  }
}

Result

Weekday

Exercise

Arrow case.

case 1  "one";

Quiz Test

Does the arrow form fall through?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.