Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between List<Number> and List<? extends Number>?

Tags:

java

generics

I have a very basic question ragarding Java generics . I thought that both List<Number> and List<? extends Number> are homogeneous . Am I right or is there something fundamental I am missing ?

like image 315
Geek Avatar asked Aug 31 '12 13:08

Geek


1 Answers

Generic types are more pedantic.

<? extends Number> means Number or an unknown a sub class. If you obtain such a value it will be a Number, but you cannot give a value of this type because you don't know which is valid.

The difference is in arguments and return values.

List<Number> numbers = new ArrayList<Number>();
Number n = 1;
numbers.add(n); // ok.
n = numbers.get(0); // ok
numbers.add(1); // ok.

List<? extends Number> numbers2 = new ArrayList<Double>();
numbers2.add(n); // not ok
n = numbers2.get(0); // ok

List<? super Number> numbers3 = new ArrayList<Serializable>();
numbers3.add(n); // ok
n = numbers3.get(0); // not ok.

super is used in a few places to signify the type can be a super type. e.g.

In Collections, this method says the Comparator needs to be able to compare the same type or any super type.

public static <T> void sort(List<T> list, Comparator<? super T> c)

This means you can have

Comparator<Number> comparesAnyNumbers = ...
List<Integer> ints = ...
Collections.sort(ints, comparesAnyNumbers);
like image 165
Peter Lawrey Avatar answered Oct 31 '22 17:10

Peter Lawrey