15

Here is my program. I am not sure why I am getting a compile time error.

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
    List< ? extends Number > list = new ArrayList<Integer>();

    list.add(6); // Compile Time Error

    System.out.println(list);

  }
}

But the following program works fine

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
    List< ? super Number > list = new ArrayList<Number>();

    list.add(6); 

    System.out.println(list);

    }
}

Error from Eclipse:

Here is the error description from Eclipse:

The method add(int, capture#1-of ? extends Number) in the type List is not applicable for the arguments (int)

asteri
  • 11,402
  • 13
  • 60
  • 84
Vikash Joshi
  • 337
  • 1
  • 9

4 Answers4

18

It's because what you are doing in the first case isn't type safe. You have declared list as a List of "some subclass of Number", and then tried to insert an Integer into it. There is absolutely no guarantee that Integer is compatible with the actual run-time type of the underlying list. The compiler stops you here because what you are doing doesn't make any sense.

Consider, as an extreme example:

List< ? extends Object > list = new ArrayList<Integer>();
list.add("Hello, World!");

If this were to work, you would have a List<Integer> with a String in it!

If you really want it to work, you have to tell the compiler you know what you are doing by casting:

((List<Integer>)list).add(6);

But even then you will still get a warning about type safety.

The second case works because the list is guaranteed to be "some superclass of Number". Integer is a subclass of Number, so it can be implicitly converted to any superclass (including Number itself), so there is no risk that the value is incompatible with the actual type of the list.

For further information, you may want to read up on the difference between covariance and contravariance.

verdesmarald
  • 11,646
  • 2
  • 44
  • 60
  • If you do something like this 'List< ? extends Object > list = new ArrayList(); ((List)list).add("Hello, World!");' should not it give ClassCastException ? – Vikash Joshi Sep 24 '12 at 15:02
  • 2
    Nope, generics are not implemented that way in Java. If you do that, you really do end up with a `List` with a `String` in it, and you will get an exception later if you try and retrieve it as an `Integer`. That is why the compiler still gives you a warning. (See [Type erasure](http://en.wikipedia.org/wiki/Type_erasure) for more details.) – verdesmarald Sep 24 '12 at 15:06
  • Thanks, but it is not clear why it is implemented in that way. – Vikash Joshi Sep 24 '12 at 15:09
  • 2
    It's complicated, but basically generics in Java only exist at compile time. At runtime (when the cast happens) the generic type is "erased" and both `List` and `List` are just `List`s, so the cast works fine. The actual exception occurs when you try to use that `String` as an `Integer`, which definitely isn't fine. – verdesmarald Sep 24 '12 at 15:14
4

You cannot add anything to a List that has a ? extends ... as part of its generic type.

Let's look at this:

List< ? extends Number > list = new ArrayList<Integer>();

Note that the actual List you're using is an ArrayList<Integer>. It should not be possible to put something else in this list that is not an Integer. Yet, the type List<? extends Number> would allow you to add for example a Double to the list, because Double also extends Number.

For more details, see the following in Angelika Langer's Java Generics FAQ: Which methods and fields are accessible/inaccessible through a reference variable of a wildcard parameterized type?

Jesper
  • 202,709
  • 46
  • 318
  • 350
1

Its about the super and the extends. Compare this Question.

you can also look it up by the Wildcards.

Community
  • 1
  • 1
Simulant
  • 19,190
  • 8
  • 63
  • 98
0

This doesn't work:

List< ? extends Number > list = new ArrayList<Integer>();
list.add(6); // Compile Time Error

because if it did you could arrive in a situation like this.

class Apple extends Number  {}

List< ? extends Number > list = new ArrayList<Integer>();
list.add(new Apple()); //if it were to work, you'd have apples in what you'd think was a list of integers
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103