Integer/Boolean Conditional-Or Operator ||
This Java operator when used as follows is called the conditional-or operator. This section is included here so that you can understand both the conditional-or operator and the escalation operator, which use the same symbol.
The || operator is like | but evaluates its right-hand operand only if
the value of its left-hand operand is false
. It is
syntactically left-associative (it groups left-to-right). It is fully
associative with respect to both side effects and result value; that is, for
any expressions
a, b,
and
c,
evaluation of the expression ((a)||(b))||(c) produces
the same result, with the same side effects occurring in the same order, as
evaluation of the expression (a)||((b)||(c)).
ConditionalOrExpression:
ConditionalAndExpression
ConditionalOrExpression || ConditionalAndExpression
Each operand of || must be of type Boolean, or a parse-time error occurs. The type of a conditional-or expression is always Boolean.
At run time, the left-hand operand expression is evaluated first; if its
value is
true
, the value of the conditional-or expression is
true
and the right-hand operand expression is not
evaluated. If the value of the left-hand operand is
false
, then the right-hand expression is evaluated and
its value becomes the value of the conditional-or expression.
Thus, || computes the same result as | on Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.