JVM Knowledge

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 a when true is definitely matched at b.
  • A pattern variable is introduced by a && b when true iff either (i) it is introduced by a when true or (ii) it is introduced by b when 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 a when true and (ii) introduced by b when true.
  • A pattern variable is both (i) introduced by a when false and (ii) introduced by b when 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 a when false is definitely matched at b.
  • A pattern variable is introduced by a || b when false iff either (i) it is introduced by a when false or (ii) it is introduced by b when 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 a when true and (ii) introduced by b when true.
  • A pattern variable is both (i) introduced by a when false and (ii) introduced by b when 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 !a when true iff it is introduced by a when false.
  • A pattern variable is introduced by !a when false iff it is introduced by a when 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 a when true is definitely matched at b.
  • A pattern variable introduced by a when false is definitely matched at c.

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 a when true and (ii) introduced by c when true.
  • A pattern variable is both (i) introduced by a when true and (ii) introduced by c when false.
  • A pattern variable is both (i) introduced by a when false and (ii) introduced by b when true.
  • A pattern variable is both (i) introduced by a when false and (ii) introduced by b when false.
  • A pattern variable is both (i) introduced by b when true and (ii) introduced by c when true.
  • A pattern variable is both (i) introduced by b when false and (ii) introduced by c when 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 a instanceof p when true iff the pattern p contains 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 throw statement.

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 by a when true.
  • A pattern variable is introduced by (a) when false iff it is introduced by a when false.