Sub topic 7
Exhaustive Switches Without `default`
Assume you know Java classes, interfaces, inheritance, switch expressions, and instanceof patterns. The examples use Java 21 or newer.
An exhaustive switch covers every value that the selector can have. A switch expression must be exhaustive because it must produce a value on every possible path. With a sealed hierarchy, the compiler can determine the permitted alternatives for you.
Sealed Types Define the Alternatives
A sealed interface limits which classes may implement it. Each permitted class must appear in the permits list or be a permitted subtype through another sealed type.
sealed interface Shape permits Circle, Rectangle, Square {}
record Circle(double radius) implements Shape {}
record Rectangle(double width, double height) implements Shape {}
record Square(double side) implements Shape {}
public class AreaExample {
static double area(Shape shape) {
return switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.width() * r.height();
case Square s -> s.side() * s.side();
};
}
public static void main(String[] args) {
System.out.println(area(new Circle(2)));
System.out.println(area(new Square(3)));
}
}The switch has no default case. It is still complete because Shape can be only a Circle, Rectangle, or Square.
If you add another permitted subtype, such as Triangle, the compiler reports that area is no longer exhaustive. You must add a Triangle case. This makes the compiler help maintain code when the data model changes.
| Selector type | How the compiler finds possible cases |
|---|---|
| Sealed interface | Its permitted direct and indirect subtypes |
| Enum | Its enum constants |
| Record pattern | Its component pattern and nested alternatives |
Why Avoid an Unnecessary `default`?
A default case can hide missing cases. For example:
return switch (shape) {
case Circle c -> 1.0;
default -> 0.0;
};This compiles, but a new Triangle silently uses default. The program may return an incorrect result without a compiler error.
Leaving out default documents that the hierarchy is intentionally closed. It also turns future additions into compile-time work. This is usually safer when every subtype needs different behavior.
A default case is still useful when:
- The hierarchy is open and unknown implementations may exist.
- You intentionally want one fallback behavior.
- The selector type is too broad to enumerate meaningfully.
- You want to handle unexpected input explicitly.
Exhaustiveness and `null`
A type pattern does not match null. If shape is null, the switch above throws NullPointerException unless the switch includes an explicit null label.
static String name(Shape shape) {
return switch (shape) {
case null -> "no shape";
case Circle c -> "circle";
case Rectangle r -> "rectangle";
case Square s -> "square";
};
}Adding case null makes the switch exhaustive for both the sealed alternatives and the null value.
Runtime Failure After Separate Compilation
A compiled exhaustive switch can encounter a subtype that did not exist when the switch was compiled. This can happen when classes are compiled separately and a sealed hierarchy changes later. In that situation, Java may throw MatchException.
This is uncommon in a consistently rebuilt application. It matters more for libraries, plugins, and deployments where related classes can be updated independently. Treat MatchException as a sign that the compiled switch and the runtime type hierarchy are out of sync.
Exercise
Why can the area switch omit default, and what happens when a new permitted subtype is added?
Show answer
The sealed interface lists every permitted subtype, so the cases are exhaustive. Adding a subtype makes the compiler require a new case.