Control Flow & Loops

The switch Statement & switch Expressions

15 min Lesson 4 of 14

The switch Statement & switch Expressions

When you need to choose between many fixed values, a chain of if / else if blocks works but quickly becomes hard to read. Java offers a better tool: the switch. Java has two flavours — the classic switch statement that has been in the language since version 1, and the modern switch expression introduced as a standard feature in Java 14. By Java 17 the modern form is the recommended default for most new code.

Classic switch Statement

The classic form tests a single value against a list of case labels and jumps to the matching one. The break at the end of each arm stops execution from continuing into the next arm.

int day = 3; String name; switch (day) { case 1: name = "Monday"; break; case 2: name = "Tuesday"; break; case 3: name = "Wednesday"; break; case 4: name = "Thursday"; break; case 5: name = "Friday"; break; default: name = "Weekend"; break; } System.out.println(name); // Wednesday

The default label is optional but strongly recommended — it handles any value that does not match a case, similar to the final else in an if-chain.

Fall-through: Feature or Bug?

If you omit break, execution falls through into the next case and keeps running until it hits a break or the end of the switch. This behaviour is intentional for grouping cases that share the same logic.

int month = 4; // April int daysInMonth; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: daysInMonth = 31; break; case 4: case 6: case 9: case 11: daysInMonth = 30; break; case 2: daysInMonth = 28; // ignoring leap years for simplicity break; default: daysInMonth = -1; } System.out.println(daysInMonth); // 30

Here months 4, 6, 9, and 11 all fall through to the same daysInMonth = 30 assignment. This is clean and deliberate. Unintentional fall-through — forgetting break — is one of the most common beginner bugs.

Missing break is a silent bug. Java does not warn you if you forget break. The code compiles, but execution continues into the next case and may assign or print the wrong value. Always double-check every arm of a classic switch.

What can go in a switch?

The switch value must be one of the following types. You cannot switch on a double, long, or a custom object.

  • byte, short, char, int (and their wrapper classes)
  • String
  • enum values

Modern switch Expression (Java 14+)

The switch expression solves three problems with the classic statement: no accidental fall-through, the result can be assigned directly to a variable, and the syntax is much shorter. Each arm uses -> instead of : and does NOT fall through.

int day = 3; String name = switch (day) { case 1 -> "Monday"; case 2 -> "Tuesday"; case 3 -> "Wednesday"; case 4 -> "Thursday"; case 5 -> "Friday"; default -> "Weekend"; }; System.out.println(name); // Wednesday

Notice that the entire switch is an expression — it produces a value that is stored in name. The semicolon goes at the very end of the assignment, not inside the arms.

Prefer switch expressions for new code. They are safer (no fall-through), shorter, and can be used directly inside other expressions — for example as an argument to a method call or as a return value.

Grouping Cases in a switch Expression

You can list multiple values in a single arrow arm by separating them with commas:

String season = switch (month) { case 12, 1, 2 -> "Winter"; case 3, 4, 5 -> "Spring"; case 6, 7, 8 -> "Summer"; case 9, 10, 11 -> "Autumn"; default -> "Unknown"; };

This replaces the empty fall-through cases of the classic form with one readable line.

Multi-line Arms with yield

When an arm needs more than a single expression — say, a local variable calculation — wrap the body in braces and use yield to return the value:

int score = 85; String grade = switch (score / 10) { case 10, 9 -> "A"; case 8 -> "B"; case 7 -> "C"; case 6 -> { System.out.println("Just passed!"); yield "D"; } default -> { System.out.println("Failed."); yield "F"; } }; System.out.println(grade); // B

yield is only used inside a block arm of a switch expression. It is different from return — it exits the switch block, not the whole method.

switch Statement vs. switch Expression — Quick Comparison

  • Classic statement — uses case X:, requires break, fall-through is possible, does not produce a value.
  • Modern expression — uses case X ->, no fall-through, produces a value, uses yield for multi-line blocks.
Exhaustiveness. When a switch expression is used on an enum, the Java compiler checks that every possible enum constant is covered. If you miss one, you get a compile error — which is far better than a runtime crash.

Summary

The classic switch statement is useful but requires careful break management and deliberate fall-through handling. The modern switch expression, available from Java 14 onwards, eliminates accidental fall-through, allows direct assignment, and keeps code concise. For the Java 17+ code you are writing in this course, prefer the arrow-form switch expression by default and reach for the classic statement only when you explicitly need fall-through behaviour.