switch
7 tutorials tagged “switch”.
- Exhaustive Switch with Sealed TypesCover every subtype of a sealed interface without default, so the compiler flags each switch to update when a type is added. Includes an expression evaluator.
- Handling Null in SwitchWhy switch throws NullPointerException for null, how case null and case null, default fix it, and how null behaves in instanceof and record patterns.
- Pattern Matching for Switch in JavaSwitch on an object's type with case patterns instead of instanceof chains: type patterns, case order and dominance, exhaustiveness, and how it compiles.
- Guards in Switch (When Clause)Add a condition to a case pattern with when: guarded patterns, their order, how they affect dominance and exhaustiveness, and the rules for guard expressions.
- Enum Methods and Enums in Switchvalues(), valueOf(), ordinal(), name() and compareTo() on enums, the exception for unknown names, enums in switch, and what the compiler generates.
- Switch StatementChoose between many fixed values with switch: case labels for int, char, String and enum, why break matters, fall-through and default.
- Switch ExpressionsThe modern switch that produces a value: arrow syntax, several labels per case, yield for multi-line cases, and exhaustiveness checks.