16.1.1. Boolean Constant Expressions
-
V is [un]assigned after any constant expression (§15.29) whose value is
truewhen false. -
V is [un]assigned after any constant expression whose value is
falsewhen true. -
V is [un]assigned after any constant expression whose value is
truewhen true iff V is [un]assigned before the constant expression. -
V is [un]assigned after any constant expression whose value is
falsewhen false iff V is [un]assigned before the constant expression. -
V is [un]assigned after a boolean-valued constant expression
eiff V is [un]assigned afterewhen true and V is [un]assigned afterewhen false.This is equivalent to saying that V is [un]assigned after
eiff V is [un]assigned beforee.
Because a constant expression whose value is true never has the value false, and a constant expression whose value is false never has the value true, the first two rules are vacuously satisfied. They are helpful in analyzing expressions involving the operators && (§16.1.2), || (§16.1.3), ! (§16.1.4), and ? : (§16.1.5).
16.1.2. Conditional-And Operator &&
- V is [un]assigned after
a&&b(§15.23) when true iff V is [un]assigned afterbwhen true. - V is [un]assigned after
a&&bwhen false iff V is [un]assigned afterawhen false and V is [un]assigned afterbwhen false. - V is [un]assigned before
aiff V is [un]assigned beforea&&b. - V is [un]assigned before
biff V is [un]assigned afterawhen true. - V is [un]assigned after
a&&biff V is [un]assigned aftera&&bwhen true and V is [un]assigned aftera&&bwhen false.
16.1.3. Conditional-Or Operator ||
- V is [un]assigned after
a||b(§15.24) when true iff V is [un]assigned afterawhen true and V is [un]assigned afterbwhen true. - V is [un]assigned after
a||bwhen false iff V is [un]assigned afterbwhen false. - V is [un]assigned before
aiff V is [un]assigned beforea||b. - V is [un]assigned before
biff V is [un]assigned afterawhen false. - V is [un]assigned after
a||biff V is [un]assigned aftera||bwhen true and V is [un]assigned aftera||bwhen false.
16.1.4. Logical Complement Operator !
-
V is [un]assigned after
!a(§15.15.6) when true iff V is [un]assigned afterawhen false. -
V is [un]assigned after
!awhen false iff V is [un]assigned afterawhen true. -
V is [un]assigned before
aiff V is [un]assigned before!a. -
V is [un]assigned after
!aiff V is [un]assigned after!awhen true and V is [un]assigned after!awhen false.This is equivalent to saying that V is [un]assigned after
!aiff V is [un]assigned aftera.
16.1.5. Conditional Operator ? :
Suppose that b and c are boolean-valued expressions.
- V is [un]assigned after
a?b:c(§15.25) when true iff V is [un]assigned afterbwhen true and V is [un]assigned aftercwhen true. - V is [un]assigned after
a?b:cwhen false iff V is [un]assigned afterbwhen false and V is [un]assigned aftercwhen false. - V is [un]assigned before
aiff V is [un]assigned beforea?b:c. - V is [un]assigned before
biff V is [un]assigned afterawhen true. - V is [un]assigned before
ciff V is [un]assigned afterawhen false. - V is [un]assigned after
a?b:ciff V is [un]assigned aftera?b:cwhen true and V is [un]assigned aftera?b:cwhen false.
Suppose that b and c are expressions that are not boolean-valued.
- V is [un]assigned after
a?b:ciff V is [un]assigned afterband V is [un]assigned afterc. - V is [un]assigned before
aiff V is [un]assigned beforea?b:c. - V is [un]assigned before
biff V is [un]assigned afterawhen true. - V is [un]assigned before
ciff V is [un]assigned afterawhen false.
16.1.6. switch Expressions
Suppose that a switch expression (§15.28) has result expressions e_1, ..., e_n, all of which are boolean-valued.
The following rules apply only if the switch block of the switch expression consists of switch labeled statement groups (§14.11.1):
- V is definitely assigned after a
switchexpression when true iff for everyyieldstatement with expressione(§14.21) in the switch block that may exit theswitchexpression, V is definitely assigned afterewhen true. - V is definitely assigned after a
switchexpression when false iff for everyyieldstatement with expressionein the switch block that may exit theswitchexpression, V is definitely assigned afterewhen false. - V is definitely unassigned after a switch expression when true iff for every
yieldstatement with expressionein the switch block that may exit theswitchexpression, V is definitely unassigned before theyieldstatement and V is definitely unassigned afterewhen true. - V is definitely unassigned after a
switchexpression when false iff for everyyieldstatement with expressionein the switch block that may exit theswitchexpression, V is definitely unassigned before theyieldstatement and V is definitely unassigned afterewhen false. - V is [un]assigned before the selector expression iff V is [un]assigned before the
switchexpression. - V is [un]assigned before the first statement of the first switch labeled statement group in the switch block iff V is [un]assigned after the selector expression.
- V is [un]assigned before the first statement of any switch labeled statement group other than the first iff V is [un]assigned after the selector expression and V is [un]assigned after the preceding statement.
The following rules apply only if the switch block of the switch expression consists of switch rules (§14.11.1):
-
V is definitely assigned after a
switchexpression when true iff for every switch rule, one of the following is true:- It introduces a switch rule expression
eand V is definitely assigned afterewhen true. - It introduces a switch rule block B and for every
yieldstatement with expressionecontained in B that may exit theswitchexpression, V is definitely assigned afterewhen true. - It introduces a switch rule
throwstatement.
- It introduces a switch rule expression
-
V is definitely assigned after a
switchexpression when false iff for every switch rule, one of the following is true:- It introduces a switch rule expression
eand V is definitely assigned afterewhen false. - It introduces a switch rule block B and for every
yieldstatement with expressionecontained in B that may exit theswitchexpression, V is definitely assigned afterewhen false. - It introduces a switch rule
throwstatement.
- It introduces a switch rule expression
-
V is definitely unassigned after a
switchexpression when true iff for every switch rule, one of the following is true:- It introduces a switch rule expression
eand V is definitely unassigned afterewhen true. - It introduces a switch rule block B and for every
yieldstatement with expressionecontained in B that may exit theswitchexpression, V is definitely unassigned before theyieldstatement and V is definitely unassigned afterewhen true. - It introduces a switch rule
throwstatement.
- It introduces a switch rule expression
-
V is definitely unassigned after a
switchexpression when false iff for every switch rule, one of the following is true:- It introduces a switch rule expression
eand V is definitely unassigned afterewhen false. - It introduces a switch rule block B and for every
yieldstatement with expressionecontained in B that may exit theswitchexpression, V is definitely unassigned before theyieldstatement and V is definitely unassigned afterewhen false. - It introduces a switch rule
throwstatement.
- It introduces a switch rule expression
-
V is [un]assigned before any switch rule expression or switch rule statement in the switch block iff V is [un]assigned after the selector expression.
Suppose that a switch expression has result expressions e_1, ..., e_n, not all of which are boolean-valued.
-
V is [un]assigned after a
switchexpression iff all of the following are true:- V is [un]assigned before every
yieldstatement that may exit theswitchexpression. - For each switch rule in the switch block, V is [un]assigned after the switch rule expression, switch rule block, or switch rule
throwstatement introduced by the switch rule.
- V is [un]assigned before every
-
V is [un]assigned before the selector expression of a
switchexpression iff V is [un]assigned before theswitchexpression. -
V is [un]assigned before the switch rule expression, switch rule block, or switch rule
throwstatement introduced by a switch rule in the switch block iff V is [un]assigned after the selector expression of theswitchexpression. -
V is [un]assigned before the first block statement of a switch labeled statement group in the switch block iff both of the following are true:
- V is [un]assigned after the selector expression of the
switchexpression. - If the switch labeled statement group is not the first in the switch block, V is [un]assigned after the last block statement of the preceding switch labeled statement group.
- V is [un]assigned after the selector expression of the
-
V is [un]assigned before a block statement that is not the first of a switch labeled statement group in the switch block iff V is [un]assigned after the preceding block statement.
16.1.7. Other Expressions of Type boolean
Suppose that e is an expression of type boolean and is not a boolean constant expression, logical complement expression !a, conditional-and expression a && b, conditional-or expression a || b, or conditional expression a ? b : c.
- V is [un]assigned after
ewhen true iff V is [un]assigned aftere. - V is [un]assigned after
ewhen false iff V is [un]assigned aftere.
16.1.8. Assignment Expressions
Consider an assignment expression a = b, a += b, a -= b, a *= b, a /= b, a %= b, a <<= b, a >>= b, a >>>= b, a &= b, a |= b, or a ^= b (§15.26).
-
V is definitely assigned after the assignment expression iff either:
ais V, or- V is definitely assigned after
b.
-
V is definitely unassigned after the assignment expression iff
ais not V and V is definitely unassigned afterb. -
V is [un]assigned before
aiff V is [un]assigned before the assignment expression. -
V is [un]assigned before
biff V is [un]assigned aftera.
Note that if a is V and V is not definitely assigned before a compound assignment such as a &= b, then a compile-time error will necessarily occur. The first rule for definite assignment stated above includes the disjunct "a is V" even for compound assignment expressions, not just simple assignments, so that V will be considered to have been definitely assigned at later points in the code. Including the disjunct "a is V" does not affect the binary decision as to whether a program is acceptable or will result in a compile-time error, but it affects how many different points in the code may be regarded as erroneous, and so in practice it can improve the quality of error reporting. A similar remark applies to the inclusion of the conjunct "a is not V" in the first rule for definite unassignment stated above.
16.1.9. Operators ++ and --
- V is definitely assigned after
++a(§15.15.1),--a(§15.15.2),a++(§15.14.2), ora--(§15.14.3) iff eitherais V or V is definitely assigned after the operand expression. - V is definitely unassigned after
++a,--a,a++, ora--iffais not V and V is definitely unassigned after the operand expression. - V is [un]assigned before
aiff V is [un]assigned before++a,--a,a++, ora--.
16.1.10. Other Expressions
If an expression is not a boolean constant expression, and is not a preincrement expression ++a, predecrement expression --a, postincrement expression a++, postdecrement expression a--, logical complement expression !a, conditional-and expression a && b, conditional-or expression a || b, conditional expression a ? b : c, assignment expression, or lambda expression, then the following rules apply:
-
If the expression has no subexpressions, V is [un]assigned after the expression iff V is [un]assigned before the expression.
This case applies to literals, names,
this(both qualified and unqualified), unqualified class instance creation expressions with no arguments, array creation expressions with initializers that contain no expressions, superclass field access expressions, unqualified and type-qualified method invocation expressions with no arguments, superclass method invocation expressions with no arguments, and superclass and type-qualified method reference expressions. -
If the expression has subexpressions, V is [un]assigned after the expression iff V is [un]assigned after its rightmost immediate subexpression.
There is a piece of subtle reasoning behind the assertion that a variable V can be known to be definitely unassigned after a method invocation expression. Taken by itself, at face value and without qualification, such an assertion is not always true, because an invoked method can perform assignments. But it must be remembered that, for the purposes of the Java programming language, the concept of definite unassignment is applied only to blank final variables. If V is a blank final local variable, then only the method to which its declaration belongs can perform assignments to V. If V is a blank final field, then only a constructor or an initializer for the class containing the declaration for V can perform assignments to V; no method can perform assignments to V. Finally, explicit constructor invocations (§8.8.7.1) are handled specially (§16.9); although they are syntactically similar to expression statements containing method invocations, they are not expression statements and therefore the rules of this section do not apply to explicit constructor invocations.
If an expression is a lambda expression, then the following rules apply:
-
V is [un]assigned after the expression iff V is [un]assigned before the expression.
-
V is definitely assigned before the expression or block that is the lambda body (§15.27.2) iff V is definitely assigned before the lambda expression.
No rule allows V to be definitely unassigned before a lambda body. This is by design: a variable that was definitely unassigned before the lambda body may end up being assigned to later on, so we cannot conclude that the variable will be unassigned when the body is executed.
For any immediate subexpression y of an expression x, where x is not a lambda expression, V is [un]assigned before y iff one of the following is true:
yis the leftmost immediate subexpression ofxand V is [un]assigned beforex.yis the right-hand operand of a binary operator and V is [un]assigned after the left-hand operand.xis an array access,yis the subexpression within the brackets, and V is [un]assigned after the subexpression before the brackets.xis a primary method invocation expression,yis the first argument expression in the method invocation expression, and V is [un]assigned after the primary expression that computes the target object.xis a method invocation expression or a class instance creation expression;yis an argument expression, but not the first; and V is [un]assigned after the argument expression to the left ofy.xis a qualified class instance creation expression,yis the first argument expression in the class instance creation expression, and V is [un]assigned after the primary expression that computes the qualifying object.xis an array creation expression;yis a dimension expression, but not the first; and V is [un]assigned after the dimension expression to the left ofy.xis an array creation expression initialized via an array initializer;yis the array initializer inx; and V is [un]assigned after the dimension expression to the left ofy.