Why the output of following code is 9.0 and not 9 ? If ternary operator is nothing but short form of if-else branch then why java compiler is promoting int to double ?
If ternary operator is nothing but short form of if-else branch then why java compiler is promoting int to double ?
A conditional expression has a single type, which both the second and third operands are converted to as necessary. The JLS gives the rules determining the expression type, which are slightly complicated due to auto-unboxing.
The conditional operator is sort of just shorthand for an if
/else
construct, but not the sort of shorthand I think you expected. So your code is equivalent to this:
double value;
if (a < 5) {
value = 9.9;
} else {
value = 9;
}
System.out.println("Value is - " + value);
It's not short for:
if (a < 5) {
System.out.println("Value is - " + 9.9);
} else {
System.out.println("Value is - " + 9);
}
For more details, see section 15.25 of the Java Language Specification.
Java needs to know the type of the result at compile time. So as this ternary operator can result an int or a double, the compiler chooses the double as the result type.
Because the type of the expression as a whole is double
, because one of the operands to the operator is a double
. The type of the expression containing the ternary is dictated by the operands, which must be of the same type. In the case of your expression, the 9
is coerced to a double
to make it the same type as the 9.9
.
Because the type of the conditional operator
(Yes, it's conditional operator and not ternary operator) in this case will be the promoted type of the 3rd operand, since 2nd and 3rd operand are not of same type.
This is clearly listed in JLS Secion - 15.25: -
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 or Byte and the other is of type short or Short, > then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression (§15.28) of type int 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, or Character, and the other operand is a constant expression (§15.28) of type int whose value is representable in the type U 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.
See the last point, that is of use here. So, in this case, as a rule of binary numeric promotion
- See JLS Section 5.6.2: -
- If either operand is of type double, the other is converted to double.
Actually the ternary operator is not strictly speaking a short form of if/else as it does perform type conversion if required. In particular, in your case, JLS 15.25 requires that:
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.
If you follow the link to §5.6.2:
If either operand is of type double, the other is converted to double.