Switch statements
What you will learn
A switch statement chooses an action from a discrete selector value, such as a menu command. It can be clearer than repeated equality tests when the choices are named constants. Java supports the traditional colon form and modern arrow rules.
public class Main { public static void main(String[] args) { int command = 2; switch (command) { case 1: System.out.println("Open"); break; case 2: System.out.println("Save"); break; default: System.out.println("Unknown"); } }}Expected output:
SaveThe second label matches; break exits the switch.
How it works
The selector inside switch is evaluated once. In this introductory constant-label subset use int, char, String or an enum (a named set of constants, studied later); byte, short and matching integral wrappers are also supported. Use case constant: with break; to stop the traditional switch after its action. Without break, execution can continue through later groups regardless of their labels. Arrow rules case constant -> statement; execute only the matched rule; commas group constants. Arrow rules became permanent in Java 14. Do not mix colon groups and arrow rules in one switch. A traditional constant-label statement may omit default, in which case a non-null unmatched selector has no action. A null String selector without case null throws NullPointerException, even with default. Java 21 supports case null and pattern switches; advanced pattern matching has additional rules and is deferred. For numeric ranges prefer if/else.
public class Main { public static void main(String[] args) { String day = "Sun"; switch (day) { case "Sat", "Sun" -> System.out.println("Weekend"); case "Mon", "Tue", "Wed", "Thu", "Fri" -> System.out.println("Weekday"); default -> System.out.println("Unknown day"); } }}Expected output:
WeekendBoth weekend labels share a rule. No break is required.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { String command = null; if (command == null) { System.out.println("Missing command"); return; } switch (command) { case "start" -> System.out.println("Start"); default -> System.out.println("Unknown command"); } }}Expected output:
Missing commandThe guard prevents selection on null; default alone would not catch it.
Common mistakes
Forgetting break can trigger unintended actions: case 1 followed by case 2 without break may print both messages. Use arrow rules for exclusive actions or document deliberate fall-through. String case labels match case-sensitive contents, not reference identity.
Try it yourself
Write a kiosk switch statement: 1 prints Browse, 2 and 3 print Support, other integers print Unknown. Use arrow rules and test 3, 1 and 9 independently.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
public class Main { @SuppressWarnings("fallthrough") public static void main(String[] args) { int code = 1; switch (code) { case 1: System.out.println("First"); // Intentional fall-through for demonstration. case 2: System.out.println("Second"); break; default: System.out.println("Other"); } }}Expected output:
FirstSecondThe selector matches case 1 and continues into case 2. The annotation suppresses the compiler warning only for this intentional demonstration; do not suppress accidental fall-through warnings in normal code.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Switch statements — knowledge check
Apply switch statements to examples and boundary conditions.
What can happen when break is omitted after a colon case?
Which syntax groups two constants in an arrow rule?
A String selector is null, with default but no case null. What happens?
Which construct directly expresses numeric ranges?
What is true of an arrow switch rule?