0

I'm wondering why this code couldn't be compiled:

private static List<? super String> x() {
      return null;
}


List<Object> l = x();

List is of type object so we can store everything in it. Who can explain?

the_kaba
  • 1,457
  • 2
  • 14
  • 31

1 Answers1

2
List<Object> != List<? super String>

You can store everything on it.

l.add("a");
l.add(1);
l.add(new Object());

But you cannot assign a List<? super String> to a List<Object> because they aren't the same thing.

You can add a BigDecimal to a List<Object>, but you cannot add a BigDecimal to a List<? super String>.

hbelmiro
  • 987
  • 11
  • 31