the_kaba
the_kaba

Reputation: 1567

Why we can't get list of Object?

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?

Upvotes: 0

Views: 156

Answers (1)

hbelmiro
hbelmiro

Reputation: 1027

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>.

Upvotes: 2

Related Questions