Sub topic 4
Refining a Case with `when` Guards
This chapter assumes you know Java classes, inheritance, instanceof, casting, and switch expressions. The examples use Java 21 or newer.
A type pattern checks an object's runtime type and binds it to a variable:
if (value instanceof String text) {
return text.length();
}A when guard adds a condition to that pattern. The case matches only when both the type pattern and the condition succeed.
How `when` guards work
The variable created by the pattern is available inside its guard. If the pattern matches but the guard is false, Java tries the next case.
static String describe(Object value) {
return switch (value) {
case Integer number when number > 100 -> "large integer";
case Integer number -> "integer";
case String text when text.isBlank() -> "blank string";
case String text -> "string with " + text.length() + " characters";
case null -> "null";
default -> "other value";
};
}For an Integer, Java first tests number > 100. A value such as 150 selects the first case. A value such as 42 fails that guard, so Java continues to the next Integer case.
For a String, the guarded case handles blank text. Nonblank text reaches the following case.
A guard is useful when the type alone is too broad. It lets one type have several meaningful branches without a separate nested if.
| Pattern | Guard | Matches |
|---|---|---|
| `Integer number` | `number > 100` | Integers greater than 100 |
| `Integer number` | none | Remaining integers |
| `String text` | `text.isBlank()` | Blank strings |
| `String text` | none | Remaining strings |
Put specific cases before general cases
Cases are checked from top to bottom. A broad case can make a later case unreachable. Java calls this a dominance error.
static String bad(Object value) {
return switch (value) {
case CharSequence sequence -> "text";
case String text -> "string";
default -> "other";
};
}This code does not compile. Every String is a CharSequence, so the first case already accepts every value that the String case could accept. The String case is dominated.
Swap the cases when the more specific type needs different behavior:
static String good(Object value) {
return switch (value) {
case String text when text.length() > 10 -> "long string";
case String text -> "string";
case CharSequence sequence -> "other character sequence";
default -> "other";
};
}The guarded String case belongs before the unguarded String case. Otherwise, every string would match the unguarded case first.
A practical rule
When designing guarded cases:
- Handle a specific condition before its type's general case.
- Handle a subtype before its supertype.
- Keep a final unguarded case for the remaining values of a type.
- Add
case nullwhen null needs explicit behavior. - Use
defaultonly for values outside the types you want to describe.
public class Main {
static String describe(Object value) {
return switch (value) {
case Integer number when number > 100 -> "large integer";
case Integer number -> "integer";
case String text when text.isBlank() -> "blank string";
case String text -> "string of length " + text.length();
case null -> "null";
default -> "other";
};
}
public static void main(String[] args) {
System.out.println(describe(150));
System.out.println(describe(42));
System.out.println(describe(" "));
System.out.println(describe("Java"));
System.out.println(describe(null));
}
}Exercise
Why must case String text when text.isBlank() appear before case String text?
Show answer
The unguarded case matches every String, so placing it first would prevent the guarded case from ever being selected.