Java / Java 21 Coding Standards Interview Questions
What happens when you omit a break statement in a traditional switch under coding standards?
In a traditional colon-style switch, omitting break causes execution to fall through into the next case's statements, continuing until a break, return, or the end of the switch block is reached - a behavior that is easy to trigger by accident.
switch (level) { case LOW: System.out.println("low"); // missing break falls through case HIGH: System.out.println("high"); break; }
Because accidental fall-through is such a common source of bugs, coding standards for Java 21 recommend the arrow-style switch (case LOW -> ...) for new code, since arrow cases never fall through - each case is a self-contained branch with no break needed at all.
When the traditional colon form must be kept for legacy reasons, standards require an explicit // fall through comment on any case that intentionally omits break, so a reviewer can tell a deliberate fall-through from a forgotten one at a glance.
More Related questions...