Skip to content

Lesson 02 · The switch — Statements & Expressions

Objectives

After this lesson you will be able to:

  • Use the traditional switch statement and explain fall-through and break.
  • Use the switch expression (->, yield) and its exhaustiveness requirement.
  • Know which types a switch accepts and how null and default behave.

The traditional switch statement

A switch statement compares a selector to case labels and jumps to the match. Without break, execution falls through into the following cases.

java
int day = 3;
switch (day) {
    case 1: System.out.println("Mon"); break;
    case 2: System.out.println("Tue"); break;
    case 3:
    case 4: System.out.println("Wed or Thu"); break;   // shared via fall-through
    default: System.out.println("other");
}

Exam trap

A missing break is the most-tested switch bug: execution continues into later cases until a break or the end. The selector accepts byte/short/char/int (and their wrappers), String, and enumnot long, float, double, or boolean. case labels must be compile-time constants.

The switch expression (Java 14+)

A switch expression produces a value, using the arrow form case L ->. There is no fall-through and no break; each arm is independent.

java
String name = switch (day) {
    case 1 -> "Mon";
    case 2 -> "Tue";
    case 3, 4 -> "Wed or Thu";       // multiple labels, comma-separated
    default -> "other";
};

For a multi-statement arm, use a block and yield to return its value:

java
int size = switch (fruit) {
    case "apple" -> 1;
    case "melon" -> {
        int kg = weigh(fruit);
        yield kg;                    // yield, not return
    }
    default -> 0;
};

Exam trap

A switch expression must be exhaustive — every possible value is covered. For an enum that means all constants or a default; for other types a default is required. A non-exhaustive switch expression does not compile. (A switch statement has no such requirement.)

Gotcha

You can use the arrow form in a switch statement too (no value, no fall-through). But you may not mix : and -> styles in one switch — that's a compile error.

null and the selector

A traditional switch on a null selector throws NullPointerException (it tries to unbox / match). Since Java 21, a switch may include an explicit case null label to handle it:

java
String s = null;
String r = switch (s) {
    case null -> "was null";
    case "hi" -> "greeting";
    default   -> "other";
};

Beyond the exam

Pattern labels in switch (case Integer i ->, case null, default ->, record deconstruction, and guarded when clauses) are covered in Module 03 · Pattern Matching. They build directly on the arrow-form switch expression shown here.

Key Takeaways

  • A traditional switch falls through without break; arms can be shared by stacking case labels.
  • A switch expression (case L -> …) yields a value, never falls through, and uses yield for block arms.
  • A switch expression must be exhaustive (all enum constants, or a default) or it won't compile; a switch statement need not be.
  • Selector types: int-family, char, String, enum (since 21 also patterns) — not long, float, double, boolean. Labels are compile-time constants.
  • A null selector throws NPE unless a case null label handles it (Java 21).

Lesson Quiz

Lesson Quiz · The switch0 / 5
  1. What does this print?

    int x = 1;
    switch (x) {
        case 1: System.out.print("a");
        case 2: System.out.print("b"); break;
        case 3: System.out.print("c");
    }
    • Aa
    • Bab
    • Cabc
    • Db
  2. Why does this switch expression NOT compile?

    int n = 2;
    String s = switch (n) {
        case 1 -> "one";
        case 2 -> "two";
    };
    • AMissing break
    • BNot exhaustive (no default)
    • Cyield required
    • Dcase values must be String
  3. Which selector type is NOT allowed in a switch?

    • AString
    • Bint
    • Clong
    • Dan enum
  4. How does a block arm of a switch EXPRESSION return its value?

    • Areturn
    • Bbreak value
    • Cyield
    • D= value
  5. What happens here in Java 21?

    String s = null;
    String r = switch (s) {
        case "hi" -> "g";
        default -> "d";
    };
    • Ar = "d"
    • Br = "g"
    • CNullPointerException
    • DCompile error

Next: Loops. Run the matching code in labs/src/main/java/com/jse21/m02_flow/Switches.java.