Why does the ternary operator unexpectedly cast integers?

I have seen it discussed somewhere that the following code results in obj being a Double, but that it prints 200.0 from the left hand side.

Object obj = true ? new Integer(200) : new Double(0.0);

System.out.println(obj);

Result: 200.0


However, if you put a different object in the right hand side, e.g. BigDecimal, the type of obj is Integer as it should be.

Object obj = true ? new Integer(200) : new BigDecimal(0.0);

System.out.println(obj);

Result: 200


I presume that the reason for this is something to do with casting the left hand side to a double in the same way that it happens for integer/double comparisons and calculations, but here the left and right sides do not interact in this way.

Why does this happen?


Solution 1:

You need to read section 15.25 of the Java Language Specification.

In particular:

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 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 Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
  • If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
  • If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
  • 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 unboxing conversion (§5.1.8) and value set conversion (§5.1.13).

So binary numeric promotion is applied, which starts with:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:

  • If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
  • If either operand is of type double, the other is converted to double.

That's exactly what happens here - the parameter types are converted to int and double respectively, the second operand (the third in the original expression) is then of type double, so the overall result type is double.

Solution 2:

Numeric conversion in the conditional operator ? :

In the conditional operator a?b:c, if both b and c are different numeric types, the following conversion rules are applied at compile time to make their types equal, in order:

  • The types are converted to their corresponding primitive ones, which is called unboxing.

  • If one operand were a constant int (not Integer before unboxing) whose value is representable in the other type, the int operand is converted into the other type.

  • Otherwise the smaller type is converted into the next greater one until both operands have the same type. The conversion orders are:
    byte -> short -> int -> long -> float -> double
    char -> int -> long -> float -> double

Eventually the whole conditional expression gets the type of its second and third operands.

Examples:
If you combine char with short, the expression becomes int.
If you combine Integer with Integer, the expression becomes Integer.
If you combine final int i = 5 with a Character, the expression becomes char.
If you combine short with float, the expression becomes float.

In the question's example, 200 is converted from Integer into double, 0.0 is unboxed from Double into double and the whole conditional expression becomes becomes double which is eventually boxed into Double because obj is of type Object.