Only certain kinds of boolean expressions are involved in introducing pattern variables and determining where those variables are definitely matched. If an expression is not a conditional-and expression, conditional-or expression, logical complement expression, conditional expression, instanceof expression, switch expression, or parenthesized expression, then no scope rules apply.
6.3.1.1. Conditional-And Operator &&
The following rules apply to a conditional-and expression a && b (§15.23):
- A pattern variable introduced by
awhen true is definitely matched atb. - A pattern variable is introduced by
a&&bwhen true iff either (i) it is introduced byawhen true or (ii) it is introduced bybwhen true.
It should be noted that there is no rule for introducing a pattern variable by a && b when false. This is because it cannot be determined at compile time which operand will evaluate to false.
It is a compile-time error if any of the following conditions hold:
- A pattern variable is both (i) introduced by
awhen true and (ii) introduced bybwhen true. - A pattern variable is both (i) introduced by
awhen false and (ii) introduced bybwhen false.
These two error cases exclude the possibility of both operands of the && operator declaring a pattern variable of the same name. For example, consider the problematic expression (a instanceof String s) && (b instanceof String s). The first error case covers the entire expression evaluating to true, where (if the code were legal) two declarations of a pattern variable s would need to be initialized, given that both the left-hand operand and the right-hand operand evaluated to true. Since there is no way to distinguish the two variables called s in the rest of the program, the entire expression is considered erroneous. The second error case covers the opposite scenario where the entire expression evaluates to false.
6.3.1.2. Conditional-Or Operator ||
The following rules apply to a conditional-or expression a || b (§15.24):
- A pattern variable introduced by
awhen false is definitely matched atb. - A pattern variable is introduced by
a||bwhen false iff either (i) it is introduced byawhen false or (ii) it is introduced bybwhen false.
It should be noted that there is no rule for introducing a pattern variable by a || b when true. This is because it cannot be determined at compile time which operand will evaluate to true.
It is a compile-time error if any of the following conditions hold:
- A pattern variable is both (i) introduced by
awhen true and (ii) introduced bybwhen true. - A pattern variable is both (i) introduced by
awhen false and (ii) introduced bybwhen false.
These two error cases exclude the possibility of both operands of the || operator declaring a pattern variable of the same name. For example, consider the problematic expression (a instanceof String s) || (b instanceof String s). The first error case covers the entire expression evaluating to true, where (if the code were legal) exactly one declaration of a pattern variable s would be initialized depending on whether the left-hand operand or the right-hand operand evaluated to true. Since it cannot be determined at compile time which operand will evaluate to true, and therefore which declaration of s will be initialized, the entire expression is considered erroneous. The second error case covers the opposite scenario where the entire expression evaluates to false.
6.3.1.3. Logical Complement Operator !
The following rules apply to a logical complement expression !a (§15.15.6):
- A pattern variable is introduced by
!awhen true iff it is introduced byawhen false. - A pattern variable is introduced by
!awhen false iff it is introduced byawhen true.
6.3.1.4. Conditional Operator ? :
The following rules apply to a conditional expression a ? b : c (§15.25):
- A pattern variable introduced by
awhen true is definitely matched atb. - A pattern variable introduced by
awhen false is definitely matched atc.
It should be noted that there are no rules for introducing a pattern variable by a ? b : c when true or false. This is because it cannot be determined at compile time whether the operand a will evaluate to true.
It is a compile-time error if any of the following conditions hold:
- A pattern variable is both (i) introduced by
awhen true and (ii) introduced bycwhen true. - A pattern variable is both (i) introduced by
awhen true and (ii) introduced bycwhen false. - A pattern variable is both (i) introduced by
awhen false and (ii) introduced bybwhen true. - A pattern variable is both (i) introduced by
awhen false and (ii) introduced bybwhen false. - A pattern variable is both (i) introduced by
bwhen true and (ii) introduced bycwhen true. - A pattern variable is both (i) introduced by
bwhen false and (ii) introduced bycwhen false.
These error cases are analogous to similar error cases for the && and || operators. They eliminate confusing cases where multiple declarations of the same pattern variable may occur across the operands of the ? : operator.
6.3.1.5. Pattern Match Operator instanceof
The following rule applies to an instanceof expression with a pattern operand, a instanceof p (§15.20.2):
- A pattern variable is introduced by
ainstanceofpwhen true iff the patternpcontains a declaration of the pattern variable (§14.30.1).
A pattern variable is not permitted to shadow another local variable (§6.4).
It should be noted that there is no rule for introducing a pattern variable by a instanceof p when false.
6.3.1.6. switch Expressions
The following rule applies to a switch expression with a switch block consisting of switch rules (§14.11.1):
- A pattern variable introduced by a switch label is definitely matched in the associated switch rule expression, switch rule block, or switch rule
throwstatement.
The following rules apply to a switch expression with a switch block consisting of switch labeled statement groups (§14.11.1):
- A pattern variable introduced by a switch label is definitely matched in all the statements of the associated switch labeled statement group.
- A pattern variable introduced by a statement S contained in a switch labeled statement group is definitely matched at all the statements following S, if any, in the switch labeled statement group.
6.3.1.7. Parenthesized Expressions
The following rules apply to a parenthesized expression (a) (§15.8.5):
- A pattern variable is introduced by
(a)when true iff it is introduced byawhen true. - A pattern variable is introduced by
(a)when false iff it is introduced byawhen false.