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.
It is a compile-time error for either the second or the third operand expression to be an invocation of a
void
method.
In fact, by the grammar of expression statements (§14.8), it is not permitted for a conditional expression to appear in any context where an invocation of a
void
method could appear.- If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
- If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression isT.
- If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
- Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
- If one of the operands is of type
byte
orByte
and the other is of typeshort
orShort
, then the type of the conditional expression isshort
. - If one of the operands is of type T where T is
byte
,short
, orchar
, and the other operand is a constant expression (§15.28) of typeint
whose value is representable in type T, then the type of the conditional expression is T. - If one of the operands is of type T, where T is
Byte
,Short
, orCharacter
, and the other operand is a constant expression (§15.28) of typeint
whose value is representable in the typeU which is the result of applying unboxing conversion to T, then the type of the conditional expression is U. - Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
- Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2.The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
At run-time, the first operand expression of the conditional expression is evaluated first. If necessary, unboxing conversion is performed on the result.
The resulting
boolean
value is then used to choose either the second or the third operand expression:
The chosen operand expression is then evaluated and the resulting value is converted to the type of the conditional expression as determined by the rules stated above.
The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.
沒有留言:
張貼留言