Sub topic 6
Exhaustive Switch Without a Default Branch
Assume you know Java interfaces, inheritance, records, switch expressions, and basic pattern matching. This chapter uses Java 21 or later because pattern matching for switch became permanent in Java 21.
A normal switch often needs a default branch. A sealed hierarchy changes that. The compiler knows every permitted subtype, so it can check that your cases cover the entire hierarchy.
Why exhaustiveness matters
An exhaustive switch handles every possible value that the compiler considers valid. If you later add a permitted subtype, existing switches can fail to compile until you decide how to handle the new case.
This makes changes visible during development instead of allowing an unhandled variant to reach production.
A pattern-matching switch over a sealed type
The following program defines a sealed interface with three permitted record implementations. The area method handles each implementation with a type pattern.
public class Main {
sealed interface Shape permits Circle, Square, Rectangle {}
record Circle(double radius) implements Shape {}
record Square(double side) implements Shape {}
record Rectangle(double width, double height) implements Shape {}
static double area(Shape shape) {
return switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Square s -> s.side() * s.side();
case Rectangle r -> r.width() * r.height();
};
}
public static void main(String[] args) {
Shape circle = new Circle(2.0);
Shape rectangle = new Rectangle(3.0, 4.0);
System.out.println(area(circle));
System.out.println(area(rectangle));
}
}Each case contains a type pattern. For example, case Circle c matches a Circle and assigns the matched object to c.
The switch has no default branch. The compiler accepts it because Shape permits exactly Circle, Square, and Rectangle. The cases cover all three possibilities.
The switch is also an expression. It produces a value that the return statement sends back to the caller.
What happens when the hierarchy changes?
Suppose you add this permitted subtype:
record Triangle(double base, double height) implements Shape {}You must also add Triangle to the permits clause. The area method then stops compiling because its switch no longer covers every permitted subtype. Add a case before compiling again:
case Triangle t -> 0.5 * t.base() * t.height();This compiler error is useful. It points to code that needs a design decision whenever the data model evolves.
| Switch design | What happens when a subtype is added |
|---|---|
| Exhaustive cases without `default` | The compiler identifies each switch that needs review |
| A `default` branch | The code may compile while silently using fallback behavior |
The `null` case is separate
Exhaustiveness over a sealed hierarchy covers the permitted non-null types. It does not automatically make null safe. Without a case null, passing null to the switch throws NullPointerException.
If null is a valid input, handle it explicitly:
static String name(Shape shape) {
return switch (shape) {
case null -> "unknown";
case Circle c -> "circle";
case Square s -> "square";
case Rectangle r -> "rectangle";
};
}If null indicates a programming error, leaving it unhandled can expose that error immediately.
When a default branch is appropriate
A default branch can still be useful when you intentionally want fallback behavior. For example, a public library may choose to return "other" for variants compiled by a newer version.
However, a default branch hides the compiler's warning when a new permitted subtype is added. Prefer explicit cases when every variant needs different behavior or when missing a case would be a bug.
Exercise
Why does the area switch compile without a default branch, and what happens after a new permitted subtype is added?
Show answer
The compiler knows every permitted subtype of Shape, and the three cases cover them all. After a new subtype is added, the switch is no longer exhaustive, so it fails to compile until a case for the new subtype is added.