Wiki Markup |
---|
The conditional operator {{?:}} uses the {{boolean}} value of one expression to decide which of the other two expressions should be evaluated \[[JLS 05|AA. Java References#JLS 05]\]. The conditional operator is syntactically right-associative. For instance {{a?b:c?d:e?f:g}} is equivalent to {{a?b:(c?d:(e?f:g))}}. |
The general form of a Java conditional expression is operand1 ? operand2 : operand3
.
- If the value of the first operand (
operand1
) istrue
, then the second operand expression (operand2
) is chosen - If the value of the first operand is
false
, then the third operand expression (operand3
) is chosen
The rules (tabulated below) used by a Java compiler to determine the type of the result of a conditional expression are quite complicated and may result in unexpected type conversions. The rules used to determine the result type of a conditional expression are given in the following table, where the first matching rule, starting from the top of the table, is usedapplied. In the table, *
refers to constant expressions of type int
(such as '0' or variables declared final
), Operand 2 refers to operand2
in the general form of a Java conditional given above, and Operand 3 refers to operand3
:
Operand 2 | Operand 3 | Resultant type |
---|---|---|
type T | type T | type T |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
other numeric | other numeric | promoted type of the 2nd and 3rd operands |
T1 = boxing conversion (S1) | T2 = boxing conversion(S2) | apply capture conversion to lub(T1,T2) |
Due to Because of the complicated nature of the rules used to determine the result type of a conditional expression and the potential for possibility of unintended type casting, it is recommended that the second and third operands of the conditional expression should always explicitly have the same type. This also applies to boxed primitives.
Noncompliant Code Example
The print statement This noncompliant code example prints the value of alpha
as A
, which is of the char
type. The third operand '0', is a constant expression of type int
whose value can be represented as a char
and hence does not cause any numeric promotion. However, this behavior depends on the value of the constant integer expression. Changing the value of the constant integer expression may lead to different behavior, as will be demonstrated in the second noncompliant code example.
...
This noncompliant example prints 65
instead of the expected A
. The print statement prints 65
, is the integer ASCII equivalent of A
. This is happens because of the numeric promotion of the second operand alpha
to an int
, which happens . The numeric promotion occurs because the third operand , (the constant expression '12345', ) is an int
that cannot be of type int
and consequently, inappropriate for being represented as a char
.
Code Block | ||
---|---|---|
| ||
public class Expr { public static void main(String[] args) { char alpha = 'A'; System.out.print(true ? alpha : 12345); } } |
...
The compliant solution casts alpha
to int
to for explicitly state stating the result type (int
) of the conditional expression. Note that while While casting 12345
to type char
would ensure ensures that both operands in the second nonconforming conditional expression have the same type (and result in A
being printed), it would result results in data loss when 12345
is converted an integer larger than Character.MAX_VAUE
is downsized to a char
. Therefore the conforming example This compliant solution casts alpha
to int
, the wider of the operand types, to avoid this issue.
Code Block | ||
---|---|---|
| ||
public class Expr { public static void main(String[] args) { char alpha = 'A'; // Cast alpha as an int to explicitly state that the type of the // conditional expression should be int. System.out.print(true ? ((int) alpha) : 12345); } } |
Noncompliant Code Example
This noncompliant code example prints 65
instead of A
. The print statement prints 65
. This is because of numeric promotion of the second operand alpha
to an int
, which happens because the third operand, variable i
, is an int
.
...
Code Block | ||
---|---|---|
| ||
public class Expr {
public static void main(String[] args) {
char alpha = 'A';
char i = 0; //declare as char
System.out.print(true ? alpha : i);
}
}
|
...
Code Block | ||
---|---|---|
| ||
public class Expr {
public static void main(String[] args) {
Integer i = Integer.MAX_VALUE;
Integer f = 0; //declare as Integer
System.out.print(true ? i : f);
}
}
|
...