Machine-translated by Google. Questions are authored in English.
Top

Discussion

What happens when a null element is unboxed from a List<Integer> into an int - which of the following is true?

  • A.The compiler rejects the assignment.
  • B.The int receives Integer.MIN_VALUE as a sentinel.
  • C.NullPointerException.
  • D.The int receives zero.

Answer: C

Correct answer: NullPointerException.

Unboxing compiles to a call of intValue() on the Integer, and invoking that on a null reference throws. The compiler cannot reject it, since List<Integer> legitimately permits null elements and nullness is not part of the type. This is the classic hazard of autoboxing: the conversion is invisible in the source and the failure appears at run time.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.