SteelReed

Search

Search posts, questions…

Java CoreQuestion 28 / 43

Middle

Why isn't List<Integer> a List<Number>? What is PECS?

Short answer

List<Integer> is not a List<Number>, even though Integer is a Number. For flexibility remember PECS: reading out uses extends, putting in uses super.

only read fromonly write toread and writeWhat does the parameterdo?? extends T? super TExact type T

3 points to say:

  1. Read only: List<? extends Number>.
  2. Write only: List<? super Integer>.
  3. Read and write: use a concrete List<T>.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.