Java Reference
In-Depth Information
The conditional-or operator is syntactically left-associative (it groups left-to-right).
The conditional-or operator 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 evalu-
ation of the expression
(
a
) || ((
b
) || (
c
))
.
Each operand of the conditional-or operator must be of type
boolean
or
Boolean
, or a compile-
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 the result has type
Boolean
, it is subjected to unboxing conversion (§
5.1.8
).
If the resulting 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; if
value becomes the value of the conditional-or expression.
Thus,
||
compures the same result as
|
on
boolean
or
Boolean
operands. It differs only in that
the right-hand operand expression is evaluated conditionally rather than always.
15.25. Conditional Operator
? :
The conditional operator
? :
uses the boolean value of one expression to decide which of
two other expressions should be evaluated.
ConditionalExpression:
ConditionalOrExpression
ConditionalOrExpression
?
Expression
:
ConditionalExpression
The conditional operator is syntactically right-associative (it groups right-to-left). Thus,
a?b:c?d:e?f:g
means the same as
a?b:(c?d:(e?f:g))
.
The conditional operator has three operand expressions.
?
appears between the first and
second expressions, and
:
appears between the second and third expressions.
The first expression must be of type
boolean
or
Boolean
, or a compile-time error occurs.
It is a compile-time error for either the second or the third operand expression to be an in-
vocation of a
void
method.