Learn Java
import com.learnjava.controlflow.Switch;

The switch Statement in Java

@Concept

A switch statement compares one value against several possible cases, running the code for whichever case matches — a more readable alternative to a long else-if chain when every branch compares the exact same variable. Traditional switch statements need an explicit break at the end of each case, or execution "falls through" into the next case's code even if its own condition doesn't match, which is a famous, easy-to-forget gotcha.

Modern Java (14+) added switch expressions using arrow syntax, case X -> ..., which don't fall through at all, can directly evaluate to a value the switch itself returns, and let the compiler confirm every possible case is handled — removing the fall-through gotcha entirely for anyone using the new form.

@HowItWorks
1

The basic structure

switch (value) { case A: ...; case B: ...; default: ...; } compares value against each case in order.

2

Fall-through without break

A traditional case without a break at its end keeps executing into the next case's code, whether or not that next case's own value actually matches.

3

default

Runs when none of the listed cases match, similar to a final else in an if/else if chain.

4

Arrow-form switch expressions (Java 14+)

case X -> ... never falls through, and the whole switch can evaluate directly to a value.

5

Exhaustiveness checking

A switch expression over an enum or sealed type can be checked by the compiler to confirm every possible case is actually handled.

@Example
public class SwitchDemo {
public static void main(String[] args) {
int day = 3;
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Unknown";
};
System.out.println(name);
}
}
Output
Wednesday
1day holds 3, so the switch expression compares it against each case in order.
2case 3 matches, so the whole expression evaluates to "Wednesday" — no fall-through risk with this arrow syntax.
3name stores that result directly, since a switch expression can be assigned like any other value.
4The single println call prints that one stored value, giving one line of output.
@Variants

Traditional switch with break

Falls through into the next case unless each one ends with an explicit break.

switch (2) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
}
Output
two

Arrow-form switch expression

Never falls through, and can evaluate directly to a value instead of just running statements.

int result = switch (2) {
case 1 -> 10;
case 2 -> 20;
default -> 0;
};
System.out.println(result);
Output
20
@UseCases
Mapping a numeric day, month, or status code to a readable label
Replacing a long else-if chain that all compares the same variable against different constants
Debugging a fall-through bug caused by a missing break in a traditional switch statement
Letting the compiler confirm every enum value is handled in a switch expression

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.