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

Discussion

Which of these statements about why an element cannot be added to a List<? extends Number> is correct?

  • A.The list is unmodifiable, so no method that changes it is available at all.
  • B.Only Number itself may be added, since that is the bound of the wildcard.
  • C.Any Number may be added, and the compiler inserts a checked cast.
  • D.The exact element type is unknown, so no value can be shown to be safe; only null may be added.

Answer: D

The wildcard says the list holds some single unknown subtype of Number, which may be List<Integer> or List<Double>, so an Integer cannot be proven acceptable. Such a list can be read from as Number, which is the usual reason for the bound; a super wildcard is what allows writing.

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

Post your comment

Please log in or register to post a comment.