The switch Statement & switch Expressions
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.
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.
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.
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)Stringenumvalues
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.
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.
Grouping Cases in a switch Expression
You can list multiple values in a single arrow arm by separating them with commas:
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:
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:, requiresbreak, fall-through is possible, does not produce a value. - Modern expression — uses
case X ->, no fall-through, produces a value, usesyieldfor multi-line blocks.
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.