What switch Is For
A switch compares one value against a list of fixed options and runs the branch that matches. When you find yourself writing a long if/else if chain that keeps testing the same variable against different constants, a switch says the same thing more clearly.
It only does equality checks against compile-time constants - it cannot test ranges or combine conditions. For those, stay with if/else.
The Classic switch Statement
The traditional form uses case labels with colons and a break after each branch:
Java evaluates day, jumps to the matching case, runs its statements, and the break exits the switch. The default branch runs when nothing else matches - think of it as the else. It is optional, but including it is good practice so unexpected values do not silently slip through.
The break Trap: Fall-Through
This is the single most common switch bug. If you leave out a break, execution does not stop at the end of a matching case - it falls through into the next case and keeps running until it hits a break or the closing brace:
You might expect just Level 1, but this prints all three lines. Because case 1 has no break, control slides straight into case 2 and case 3. Always add break unless you genuinely want fall-through - and when you do rely on it, leave a comment so the next reader knows it was on purpose.
Grouping Cases
Fall-through has one legitimate, tidy use: stacking case labels so several values share one block. Put the labels back to back with no code between them:
Here 'A', 'B', and 'C' all run the same "Pass" line. This is the intended way to say "any of these values does the same thing".
switching on Strings
You are not limited to numbers. Since Java 7 you can switch on a String, which is perfect for menu choices or command names:
Matching is case-sensitive - "Stop" would not match "stop", so normalize first (e.g. command.toLowerCase()) if the input casing varies. One more gotcha: if command is null, the switch throws a NullPointerException, so guard against null before you reach it.
The Modern Arrow Syntax
Newer Java (14+) adds an arrow form, case label -> ..., that fixes the fall-through trap by design. Each arrow case runs exactly one branch and never falls through, so there is no break to forget:
Notice you group values with a comma (case 6, 7) instead of stacking labels, and there is not a single break in sight. For multiple statements, use a block: case 1 -> { ...; ... }. Prefer this form in new code - it is shorter and removes a whole class of bugs.
switch as an Expression
The arrow form can also produce a value. A switch expression returns a result you can assign straight to a variable - no temporary, no repeated assignment in every branch:
The whole switch (...) { ... } evaluates to a number stored in days. Note the semicolon after the closing brace - it is part of the assignment statement. A switch expression must be exhaustive (cover every possible value), which is why default is here - and notice that an unmatched branch can throw an exception instead of returning a value. If a branch needs multiple statements before returning a value, use a block with yield:
case 2 -> {
boolean leap = (year % 4 == 0);
yield leap ? 29 : 28;
}
yield is how an arrow block hands back its value, the expression-world equivalent of break carrying a result.
Next: for Loops
switch picks one branch out of many; sometimes you instead need to repeat a block many times. The for loop runs code a controlled number of times - counting through a range or stepping over each item - and it is the next page.
Frequently Asked Questions
When should I use switch instead of if-else in Java?
Use switch when you are comparing one value against many fixed, constant options - like a day number, a menu choice, or an enum. It reads more clearly than a long if/else if chain and signals "pick one branch out of these known values". Stick with if/else when your conditions involve ranges (x > 10), multiple variables, or anything that is not a simple equality check against a constant.
Why do I need break in a Java switch statement?
In the classic colon syntax, once a case matches, execution falls through into the following cases until it hits a break or the end of the switch. break stops that. Forgetting it is a classic bug - you match case 1: and accidentally also run the code for case 2:, case 3:, and so on. The newer arrow syntax (case 1 -> ...) never falls through, so you do not write break at all.
Can a Java switch statement work on Strings?
Yes. Since Java 7 you can switch on a String, and you can also switch on int, char, byte, short, their wrapper types, and enum values. You cannot switch on long, double, float, or boolean. String matching is case-sensitive, so "Yes" and "yes" are different cases.