Appearance
Lesson 02 · The switch — Statements & Expressions
Objectives
After this lesson you will be able to:
- Use the traditional
switchstatement and explain fall-through andbreak. - Use the
switchexpression (->,yield) and its exhaustiveness requirement. - Know which types a
switchaccepts and hownullanddefaultbehave.
The traditional switch statement
A switch statement compares a selector to case labels and jumps to the match. Without break, execution falls through into the following cases.
java
int day = 3;
switch (day) {
case 1: System.out.println("Mon"); break;
case 2: System.out.println("Tue"); break;
case 3:
case 4: System.out.println("Wed or Thu"); break; // shared via fall-through
default: System.out.println("other");
}Exam trap
A missing break is the most-tested switch bug: execution continues into later cases until a break or the end. The selector accepts byte/short/char/int (and their wrappers), String, and enum — not long, float, double, or boolean. case labels must be compile-time constants.
The switch expression (Java 14+)
A switch expression produces a value, using the arrow form case L ->. There is no fall-through and no break; each arm is independent.
java
String name = switch (day) {
case 1 -> "Mon";
case 2 -> "Tue";
case 3, 4 -> "Wed or Thu"; // multiple labels, comma-separated
default -> "other";
};For a multi-statement arm, use a block and yield to return its value:
java
int size = switch (fruit) {
case "apple" -> 1;
case "melon" -> {
int kg = weigh(fruit);
yield kg; // yield, not return
}
default -> 0;
};Exam trap
A switch expression must be exhaustive — every possible value is covered. For an enum that means all constants or a default; for other types a default is required. A non-exhaustive switch expression does not compile. (A switch statement has no such requirement.)
Gotcha
You can use the arrow form in a switch statement too (no value, no fall-through). But you may not mix : and -> styles in one switch — that's a compile error.
null and the selector
A traditional switch on a null selector throws NullPointerException (it tries to unbox / match). Since Java 21, a switch may include an explicit case null label to handle it:
java
String s = null;
String r = switch (s) {
case null -> "was null";
case "hi" -> "greeting";
default -> "other";
};Beyond the exam
Pattern labels in switch (case Integer i ->, case null, default ->, record deconstruction, and guarded when clauses) are covered in Module 03 · Pattern Matching. They build directly on the arrow-form switch expression shown here.
Key Takeaways
- A traditional
switchfalls through withoutbreak; arms can be shared by stackingcaselabels. - A
switchexpression (case L -> …) yields a value, never falls through, and usesyieldfor block arms. - A switch expression must be exhaustive (all enum constants, or a
default) or it won't compile; a switch statement need not be. - Selector types:
int-family,char,String,enum(since 21 also patterns) — notlong,float,double,boolean. Labels are compile-time constants. - A
nullselector throws NPE unless acase nulllabel handles it (Java 21).
Lesson Quiz
What does this print?
int x = 1; switch (x) { case 1: System.out.print("a"); case 2: System.out.print("b"); break; case 3: System.out.print("c"); }Why does this switch expression NOT compile?
int n = 2; String s = switch (n) { case 1 -> "one"; case 2 -> "two"; };Which selector type is NOT allowed in a switch?
How does a block arm of a switch EXPRESSION return its value?
What happens here in Java 21?
String s = null; String r = switch (s) { case "hi" -> "g"; default -> "d"; };
Next: Loops. Run the matching code in labs/src/main/java/com/jse21/m02_flow/Switches.java.