Quick revision
The short answer
A case label dominates a later one when every value that could match the later label would already have matched the earlier one. CharSequence matches every String, so a String case placed after it could never run, and the compiler rejects it as dead code. The fix is to order cases from the most specific to the most general, with guarded cases before unguarded ones of the same type and default last.
What to remember
In this answer, we saw that a pattern switch runs the first matching case, which is why order matters. A label dominates a later one when it matches everything the later one could match, so case CharSequence cs before case String s leaves the String case unreachable and javac rejects it. Guarded patterns never dominate unguarded ones of the same type and belong first, patterns dominate constants of their type, and default must follow any pattern labels. Writing cases from specific to general avoids all of it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A case label dominates a later one when every value that could match the later label would already have matched the earlier one. CharSequence matches every String, so a String case placed after it could never run, and the compiler rejects it as dead code. The fix is to order cases from the most specific to the most general, with guarded cases before unguarded ones of the same type and default last.
Why this question is asked
Pattern matching for switch, final since Java 21, lets a single switch test the type of a value and bind it to a variable. Because cases are tried from top to bottom, the order of the cases now carries meaning, and a careless order can hide a case so that it never runs. Interviewers ask about dominance because it shows whether we understand how a pattern switch picks its branch, and because the compiler error it produces confuses people the first time they see it. In this answer, we will recap how a pattern switch chooses a case, look at the broken example and the exact reason it fails, then cover guarded patterns, constant labels, and where default and case null may go.
1A Quick Recap of Pattern Switches
A type pattern such as String s does two things at once: it tests whether the value is a String, and if it is, it declares a variable s of type String holding that value. In a switch, each case can carry a pattern, and the switch runs the first case whose pattern matches. This first-match rule is what makes order matter.
A guard is an extra condition written with the keyword when, as in case String s when s.isEmpty(). The case matches only if the pattern matches and the condition is true. A constant label is the old kind of case, such as case "yes" or case RED, which matches one exact value.
Dominance is the compiler's name for one label making another unreachable. Label A dominates label B if every value that B could match is also matched by A. If A comes first, B is dead code, and Java treats dead code in a switch as a mistake rather than something to ignore.
2Reproducing the Error
Here is the switch from the question. String implements the CharSequence interface, so every String is also a CharSequence.
The compiler stops at the String case. Think about what would happen at run time if it compiled. For describe("hi"), the switch tries the first case, asks whether "hi" is a CharSequence, gets yes, and returns "text of length 2". The String case is never even looked at. That is true for every possible String, so the second case can never run. Rather than let us ship a branch we probably believe is active, javac refuses the program.
The fix is to put the more specific type first. Now a String stops at the String case, and other kinds of text, such as a StringBuilder, fall through to the CharSequence case.
3Ordering From Specific to General
Once the order is reversed, the switch compiles and each branch has a value that reaches it.
| Earlier label | Later label | Result |
|---|---|---|
case CharSequence cs | case String s | error: String is always a CharSequence |
| case String s | case CharSequence cs | fine: StringBuilder still reaches it |
| case String s | case String s when s.isEmpty() | error: the guarded case can never run |
case String s when s.isEmpty() | case String s | fine: empty strings stop first |
| case String s | case "yes" | error: the pattern catches the constant |
| default | case Integer i | error: default must follow patterns |
Notice that the CharSequence case does not dominate anything after it except values it already catches, and default comes last. A useful habit is to read the cases as a funnel: narrow types at the top, wider types below, and the catch-all at the bottom.
The same rule covers record patterns. A type pattern case Point p matches every Point, so a later case Point(var x, var y) is dominated by it. The record pattern has to come first if we want it.
4Guards Do Not Dominate
A guarded pattern matches only some values of its type, so the compiler does not treat it as covering the whole type. That means a guarded case never dominates an unguarded case of the same type, and the guarded one should come first.
If we swap the two String cases, we get the dominance error again, because case String s already catches every empty string before the guard is ever evaluated. Notice also that the compiler does not try to reason about the guard's logic. It will not prove that case Integer i when i > 0 and case Integer i when i <= 0 together cover all integers, so we still need an unguarded case or a default.
5Constant Labels, default and case null
Constant labels follow the same idea. A pattern case String s placed before case "yes" would catch "yes" first, so that order is an error. A constant placed before the pattern is fine and is the normal way to give a few special values their own branch.
For default, the rule depends on the kind of switch. In an old-style switch with only constant labels, default may appear anywhere, which keeps old code compiling. Once the switch has pattern labels, default must come after them, because default matches everything and would otherwise dominate them. The same applies to the combined label case null, default.
case null is how a pattern switch handles a null selector. Without it, a switch on a null value throws NullPointerException, as switches always have. With it, null gets its own branch. A type pattern never matches null by itself, so case null does not conflict with the patterns; it is commonly written first, or merged with default at the end as case null, default -> ...
Notice how this switch reads from the most specific value down to the catch-all. That shape is the practical summary of every dominance rule: if we always write narrow before wide, we will rarely see the error at all.
6Follow-up questions
Is dominance checked only for types in the same hierarchy?
Yes. Dominance only exists when one label's set of values contains the other's. case String s and case Integer i match disjoint sets, so either order compiles. The check uses subtyping: a type pattern for T dominates a pattern for any subtype of T, and it dominates constant labels whose value is a T.
Does dominance apply to switch statements as well as switch expressions?
Yes. The rules are about switch labels, not about whether the switch produces a value. A pattern switch statement with a dominated case fails to compile in exactly the same way. Switch statements that use patterns must also be exhaustive, which is a separate check.
Why does the compiler not just warn instead of failing?
Because a dominated case is almost always a bug. The author wrote a branch expecting it to run, and it silently never would. Making it an error forces us to see the mistake at compile time, the same reason Java rejects unreachable statements after a return.
7Conclusion
In this answer, we saw that a pattern switch runs the first matching case, which is why order matters. A label dominates a later one when it matches everything the later one could match, so case CharSequence cs before case String s leaves the String case unreachable and javac rejects it. Guarded patterns never dominate unguarded ones of the same type and belong first, patterns dominate constants of their type, and default must follow any pattern labels. Writing cases from specific to general avoids all of it.
Open with
A pattern switch picks the first case whose pattern matches, so order matters. A case label dominates a later one if every value that could match the later label would already match the earlier one, and then the later case is dead code, which the compiler rejects with an error saying the label is dominated.
Then, if they want more
In the example, String implements CharSequence, so case CharSequence cs catches every String and case String s could never run. The fix is to order cases from most specific to most general: String first, then CharSequence, then default. A few related rules follow from the same idea.
A guarded pattern like case String s when s.isEmpty() does not dominate an unguarded case String s, because it only matches some strings, so the guarded case has to come first; the other order is an error. A type pattern dominates constant labels of that type, so case "yes" must come before case String s.
And once a switch has pattern labels, default, or case null, default, must come after them, while in an old constants-only switch default can still sit anywhere for compatibility. In practice I just write cases as a funnel, narrow at the top and the catch-all at the bottom.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain dominance rules in pattern switches and why case CharSequence cs -> ...; case...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/cd1f7075dd/