Java Reference ยท Keywords and Statements
The switch statement and expression
Reference for Java switch: classic statements with fall-through, arrow-form switch expressions, yield, pattern matching, guards and exhaustiveness.
switch selects one of several branches based on the value of a selector expression. Java has two forms. The classic statement uses case X: labels and falls through to the next label unless you break. The modern form uses arrows (case X ->), never falls through, and can be used as an expression that produces a value.
Since Java 21 a switch can also match on types and records with patterns, add a when guard, and handle null. A switch expression must be exhaustive: every possible selector value needs a matching branch.
String label = switch (day) { case 6, 7 -> "weekend"; default -> "weekday"; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
switch (selector) { case constant1, constant2 -> expressionOrBlockOrThrow; case Type name when guard -> ...; default -> ...; } switch (selector) { // classic case constant: statements default: statements }
| Part | Description |
|---|---|
selector |
byte, short, char, int, their wrappers, String, an enum, or any reference type when patterns are used. long, float, double and boolean are not allowed. |
case labels |
Compile-time constants (or enum names, written unqualified), patterns, or null. Several constants may share one case separated by commas. |
when |
Optional boolean guard on a pattern label (Java 21+). |
default |
Matches everything not matched above. |
Usage notes
- Classic labels fall through. Without
break,returnorthrow, execution continues into the next case. Arrow labels never fall through and never needbreak. - Do not mix
case X:andcase X ->in the same switch; it does not compile. - Switch expressions must be exhaustive. Cover all enum constants or sealed subtypes, or add
default. With a sealed hierarchy and nodefault, adding a new subtype turns the switch into a compile error, which is usually what you want. - Producing a value from a block uses
yield. In an arrow form a plain expression needs noyield. nullselector: withoutcase nullanullselector throwsNullPointerException. Withcase null(Java 21+) it is handled explicitly.- Strings are compared with
equals(), soswitchon a string is safe and case-sensitive. - Pattern order matters. A more general pattern before a more specific one is a compile error (dominance), and guards are checked in order.
- Scope: in a classic switch all cases share one block, so a variable declared in one case is visible in the next. Arrow bodies with braces have their own scope.
- Switch expression vs statement: an expression produces a value and must be exhaustive; an old-style statement over an enum or string is not required to be.
Related
yield,ifandelse,breakandcontinue,default- Lessons: switch, Pattern matching, Sealed classes