zencv zencv - 1 year ago 70
Java Question

Why Java compiler restricts adding even the right type of element to the List when using wildcard with extends

I understand the PECS rule with regards to extend and super from other posts, but I am a bit confused about the restrictions below code

List<Integer> integerList = new ArrayList<>();
// integerList.add(3.14); => understandably this will not compile,
// as we cannot add double to a list of Integer

List<? extends Number> numberList = integerList;
// numberList.add(3); => will not compile!
// Why the compiler cannot infer from the above line that
// adding an Integer to a list of Integer is OK, the same
// way it inferred that line 3 was not OK?

Are there any reasons why the compiler would impose the restriction on line 5, given that we are just trying to add an Integer to a list of Integers and there is sufficient information available to the compiler?

Answer Source

You are effectively saying this is a List of Something that is a subclass of Number.

So that could be anything, what if it is a List of Doubles? List of BigDecimals? This is why adding Integers is not allowed.

If you want a List that can hold any Number, that is:

List<Number> numberList = ...

In Java, there is no type-inference, except with Lambdas, so the information that the List is acutally a List of Integers is lost as soon as you cast it.