The Java switch Statement: Four Forms of switch and Fall-Through
Java's switch statement explained: arrow-form switch statements for menus, the classic colon form with break and fall-through, -Xlint:fallthrough, the four forms of switch (statement or expression, arrow or colon), why yield exists, and which form to choose.
kvi5hnuCode Sprout team member7 min readOn this page
In the switch expressions article, you used switch to compute a value. A switch statement instead performs an action for each case. It's handy when one value decides among several things to do, for example in a text menu:
Choose a drink (1, 2, 3, 4)
1. Espresso 2. Latte 3. Cappuccino 4. Cancel
A long if/else if chain testing choice == 1, choice == 2, … works, but a switch statement states the intent more directly.
The Arrow Form (Java 14+)
Scanner in = new Scanner(System.in);
System.out.print("Choose a drink (1, 2, 3, 4) ");
int choice = in.nextInt();
switch (choice) {
case 1 -> System.out.println("Espresso");
case 2, 3 -> {
System.out.println("Milk drink");
System.out.println(" (recipe " + choice + ")");
}
case 4 -> System.out.println("Cancelled");
default -> System.out.println("Bad input");
}
It looks like a switch expression, with two differences:
- A switch statement has no value; each case carries out an action. There's no assignment in front and no semicolon after the closing brace.
- A switch statement doesn't have to be exhaustive when switching on
int,String, or enum values with constant labels: if nothing matches and there's nodefault, nothing happens. (Switches that use patterns orcase nullmust be exhaustive even as statements; patterns are covered in the inheritance part.)
The other rules are the same as before: labels are compile-time constants, several labels can share a case (case 2, 3 ->), a case body is a single statement, a block, or a throw, and arrow cases never fall through.
public class Solution {
public static void main(String[] args) {
for (int choice = 1; choice <= 5; choice += 2) {
switch (choice) {
case 1 -> System.out.println("Espresso");
case 2, 3 -> {
System.out.println("Milk drink");
System.out.println(" (recipe " + choice + ")");
}
case 4 -> System.out.println("Cancelled");
default -> System.out.println("Bad input: " + choice);
}
}
}
}
Espresso
Milk drink
(recipe 3)
Bad input: 5
The Classic Form: Colons and break
Since Java 1.0 (inherited from C), switch also has a colon form. You'll see it everywhere in existing code:
switch (choice) {
case 1:
System.out.println("Espresso");
break;
case 2:
System.out.println("Latte");
break;
case 3:
System.out.println("Cappuccino");
break;
case 4:
System.out.println("Cancelled");
break;
default:
System.out.println("Bad input");
}
The behaviour is different from the arrow form in an important way. A colon label is just an entry point. Execution jumps to the matching label and then continues through the following statements, past other labels, until it reaches a break or the end of the switch. If no label matches, execution starts at default: (if present).
Fall-through
If you forget a break, execution falls through into the next case:

public class Solution {
@SuppressWarnings("fallthrough") // deliberate demo; remove this line to see the -Xlint warning shown below
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
break;
case 4:
System.out.println("four");
default:
System.out.println("default");
}
int month = 2;
int days;
switch (month) {
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
days = 28;
break;
default:
days = 31;
}
System.out.println(days);
}
}
two
three
28
Fall-through is plainly dangerous: a missing break is a classic, hard-to-spot bug. Its one legitimate use, sharing code between several labels (case 4: case 6: case 9: case 11: above), is better written today with comma-separated labels (case 4, 6, 9, 11 ->).
Let the compiler warn you
Compile with the -Xlint:fallthrough option, and javac warns about every case that can fall into the next one:
javac -Xlint:fallthrough Menu.java
Menu.java:7: warning: [fallthrough] possible fall-through into case
If a method really needs fall-through, mark it with the annotation @SuppressWarnings("fallthrough") to silence the warning for that method. (Annotations supply extra information to the compiler and other tools; they get their own part of the course.) IDEs flag fall-through by default.
The Four Forms of switch
Since Java 14, there are two independent choices, which gives four forms:
No fall-through (->) | Fall-through (:) | |
|---|---|---|
| Expression (has a value) | int n = switch (s) { case "Middle" -> 3; default -> -1; }; | int n = switch (s) { case "Middle": yield 3; default: yield -1; }; |
| Statement (performs actions) | switch (c) { case 1 -> start(); default -> help(); } | switch (c) { case 1: start(); break; default: help(); } |
Here are both expression forms producing the same result, including a branch with an extra statement:
public class Solution {
public static void main(String[] args) {
String zone = "Core";
int fee = switch (zone) {
case "Core" -> {
System.out.println("city centre!");
yield 5;
}
case "Inner", "Outer" -> 5;
case "Middle" -> 3;
default -> -1;
};
System.out.println(fee);
int fee2 = switch (zone) {
case "Core":
System.out.println("city centre!");
case "Inner", "Outer":
yield 5;
case "Middle":
yield 3;
default:
yield -1;
};
System.out.println(fee2);
}
}
city centre!
5
city centre!
5
In the colon form, "Core" prints its message and then falls through to yield 5. That's legal, but it's exactly the kind of subtlety that makes code hard to read.
yield versus break and return
- In a switch expression, every branch must
yielda value (or throw an exception). With arrows, a plain expression after->yields it implicitly; a block needs an explicityield. With colons, every branch usesyield. - Like
break,yieldends the current case; unlikebreak, it also supplies the value. - You can't jump out of a switch expression with
return,breakorcontinue. The expression must produce a value or throw:
int f(String s) {
return switch (s) {
case "a" -> 1;
default -> { return -1; } // ERROR: attempt to return out of a switch expression
};
}
- In a switch statement,
breakexits the switch, andreturnexits the whole method, as usual.
Which Form Should You Use?
- Avoid the fall-through (colon) forms in new code. Fall-through is very rarely needed, and multiple labels per case cover the common reason for it.
- Prefer switch expressions over statements when every branch computes a value for the same variable. Compare:
// Better: one assignment, and the compiler checks exhaustiveness
fee = switch (zone) {
case "Core", "Inner", "Outer" -> 5;
case "Middle" -> 3;
default -> -1;
};
// Worse: the assignment is repeated in each branch, and a missing case goes unnoticed
switch (zone) {
case "Core", "Inner", "Outer" -> fee = 5;
case "Middle" -> fee = 3;
default -> fee = -1;
}
- Use an arrow switch statement when the branches do different things (call different methods, print different messages) rather than compute one value.
- Use
if/else ifwhen the conditions aren't simple matches of one value against constants (ranges such asx > 100, or several variables), unless you use pattern matching with guards (covered later).
Modern IDEs can convert between forms automatically. IntelliJ IDEA offers "Replace with enhanced 'switch' statement" or "Replace with 'switch' expression" on old-style switches.
Common questions
Can I mix case X -> and case Y: in one switch?
No. That is a compile error ("different case kinds used in the switch"). Choose one style for the whole switch. Arrow labels are recommended in new code.
Does a switch statement need a default?
Not for int, String or enum selectors with constant labels. Unmatched values simply do nothing. A default that logs or throws is still good practice when an unexpected value would be a bug. Switch statements that use patterns or case null must be exhaustive, just like switch expressions.
Why does javac warn about fall-through only with -Xlint?
Fall-through is legal Java, and decades of existing code rely on it, so warning by default would be noisy. Turn on -Xlint:fallthrough (or all -Xlint warnings) in your build. IDEs and linters highlight it anyway.
Can I use return inside a switch statement?
Yes. In a switch statement, return exits the whole method, as anywhere else. It is only in a switch expression that return, break and continue aren't allowed to jump out, because the expression must produce a value.
Is there a reason to keep old colon-style switches?
Only for compatibility with code that has to compile on Java 13 or earlier. Otherwise, convert them. IDEs do it automatically, and the arrow form removes the risk of a forgotten break.
Summary
- A switch statement performs an action per case; a switch expression produces a value.
- Arrow cases (
->) never fall through. Colon cases are entry points: execution continues untilbreak(statement),yield(expression), or the end. - A missing
breakcauses fall-through, a classic bug; compile with-Xlint:fallthroughto be warned. Use@SuppressWarnings("fallthrough")when it's intended. - There are four forms: expression/statement × arrow/colon. You can't mix arrows and colons in one switch.
yieldsupplies a switch expression's value;return,breakandcontinuecan't jump out of a switch expression.- Prefer arrow forms, and prefer switch expressions when computing a value.
Practice Questions
What is printed? int x = 1; switch (x) { case 1: System.out.print("A"); case 2: System.out.print("B"); break; case 3: System.out.print("C"); }
AB. Execution enters at case 1, prints A, falls through to case 2, prints B, then break exits.
Rewrite this classic switch using the arrow form: switch (day) { case 6: case 7: System.out.println("Weekend"); break; default: System.out.println("Weekday"); }
switch (day) {
case 6, 7 -> System.out.println("Weekend");
default -> System.out.println("Weekday");
}
A switch statement over an int has cases 1–3 and no default. The value is 9. What happens?
Nothing. No case matches and there's no default, so the switch statement does nothing. (A switch expression without default wouldn't compile.)
Why should you prefer x = switch (...) { ... }; over a switch statement that assigns x in each case?
The expression form states that the purpose is computing one value, avoids repeating the assignment, and the compiler requires it to be exhaustive. A forgotten case becomes a compile error instead of leaving x unassigned or stale.
What compiler option warns about missing break statements, and how do you suppress it for a method where fall-through is intended?
javac -Xlint:fallthrough. Suppress it for one method with @SuppressWarnings("fallthrough").
Previous: Java Loops: while, do-while and for
Related reading

More in Core Java I
Java volatile, Atomics, Deadlocks and ThreadLocal: Safe Sharing Between Threads
Share data between Java threads without bugs: memory visibility and volatile, final fields, AtomicInteger and compareAndSet, LongAdder, lazy holder initialization, safe publication, lock-ordering deadlocks, ThreadLocal, InheritableThreadLocal and ScopedValue.
Java Condition Objects and the synchronized Keyword: await, signalAll, wait and notify
Make threads wait for a condition safely with Condition.await and signalAll, then learn Java's built-in alternative: synchronized methods and blocks, intrinsic locks, wait and notifyAll, choosing lock objects, client-side locking, and the monitor concept.
Java Race Conditions and ReentrantLock: Synchronizing Shared Data
Why unsynchronized threads corrupt shared data, shown step by step with a lost-update race, and how to fix it with ReentrantLock: the lock/try/finally idiom, critical sections, reentrancy and hold counts, tryLock, fair locks, and the mistakes that cause bugs.
Java invokeAll, invokeAny and Fork-Join: Running Groups of Tasks
Coordinate many concurrent tasks in Java: invokeAll to wait for every result, invokeAny to take the first success, timeouts, ExecutorCompletionService for results in completion order, structured concurrency, and the fork-join framework with RecursiveTask, RecursiveAction and work stealing.
Discussion
Sign in to ask a question or share what you learned.