views:

79

answers:

1

Possible Duplicates:
Booleans, conditional operators and autoboxing
Java, Google Collections Library; problem with AbstractIterator?

The code below produces a NPE:

Integer test = null;
Integer test2 = true ? test : 0;
System.out.println(test2);

To correctly print out "null" without an exception requires this code:

Integer test = null;
Integer test2 = true ? test : (Integer)0;
System.out.println(test2);

It's obvious in the first example that "test" is being unboxed (converted to native int), but why? And why does changing the other expression in the ternary operator (as in the 2nd example) fix it? Can anyone provide some kind of narrative of exactly when, what, and why stuff in both of the examples gets boxed and unboxed?

+11  A: 

From section 15.25 of the Java Language Specification:

The type of a conditional expression is determined as follows:

  • 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 type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.
    • 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 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 it's following the final bullet, performing binary numeric promotion, which performs an unboxing conversion. So the type of the conditional operator expression is int, even though you're assigning it to an Integer. It's trying to perform the unboxing conversion on null, hence the exception.

Jon Skeet