Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Explain PECS with the signature of Collections.copy, and say which of these compile:

Java
List<? extends Number> a = new ArrayList<Integer>();
a.add(1);
List<? super Integer> b = new ArrayList<Number>();
b.add(1);
Integer x = b.get(0);

PECS means producer extends, consumer super: a parameter we read T values from should be ? extends T, and one we write T values into should be ? super T. Collections.copy shows both: public static <T> void copy(List<? super T> dest, List<? extends T> src). In the snippet, a.add(1) does not compile because a might really be a List<Double>, b.add(1) compiles because any List<? super Integer> accepts Integers, and Integer x = b.get(0) does not compile because reading from a List<? super Integer> only gives an Object.

Why this question is asked

Wildcards are where many developers stop being comfortable with generics. The rules about what we can add and what we can read seem arbitrary until we see the one idea behind them, and then they become predictable. Interviewers ask this question because it combines the principle, a real JDK signature and a set of compile-or-not puzzles that can only be answered by reasoning. In this answer, we will recap wildcards, explain PECS with the reason behind each half, read Collections.copy's signature, and then go through the snippet line by line.

1A Quick Recap of Wildcards

Generic types are invariant: a List<Integer> is not a List<Number>. Wildcards relax that. List<? extends Number> means a list of some unknown type that is Number or a subtype of it, so a List<Integer>, List<Double> or List<Number> all fit. List<? super Integer> means a list of some unknown type that is Integer or a supertype of it, so a List<Integer>, List<Number> or List<Object> all fit.

The key word is unknown. The compiler knows the bound, but not the actual type of the list it will be given. Everything that follows comes from asking: what operations are safe for every list that could be passed here?

2Producer Extends, Consumer Super

A producer is a parameter that gives us values: we read from it. If a list is a List<? extends Number>, every element, whatever the real list type, is at least a Number, so reading a Number is always safe. Writing is not, because we do not know whether the list holds Integers or Doubles, so any value we add might be the wrong kind.

A consumer is a parameter that takes values from us: we write into it. If a list is a List<? super Integer>, its element type is Integer or something above it, so an Integer fits in all of them, and writing an Integer is always safe. Reading gives us something of the unknown type, which could be Integer, Number or Object, so the only type the compiler can promise is Object.

That is the whole of PECS, a phrase from Effective Java. Producers use extends because we read from them, consumers use super because we write into them, and a parameter that does both should have an exact type, with no wildcard.

What a bounded wildcard allows
Declared typeRead givesCan add
List<? extends Number>Numberonly null
List<? super Integer>ObjectInteger (and its subtypes)
List<Integer>IntegerInteger
extends makes a safe source, super makes a safe destination.

3Reading the Signature of Collections.copy

Collections.copy copies every element of one list into another, overwriting positions from index 0. Its signature uses both halves of PECS.

Java
public static <T> void copy(List<? super T> dest, List<? extends T> src)
List<Integer> src = List.of(1, 2, 3);
List<Number> dest = new ArrayList<>(List.of(0.0, 0.0, 0.0, 9.9));
Collections.copy(dest, src);
System.out.println(dest); // [1, 2, 3, 9.9]: T is inferred as Integer
Step throughHow copy uses each parameter
copysrc: List<? extends T>dest: List<? super T>
get(i)
a value of type T
set(i, value)
accepted: dest holds T or a supertype

4 messages in order. Play them, or step through them one at a time.

Reads come from the extends side, writes go to the super side.

src is a producer, the method only reads T values from it, so it is List<? extends T>. dest is a consumer, the method only writes T values into it, so it is List<? super T>. With T inferred as Integer, src can be a List<Integer> and dest can be a List<Number> or List<Object>. Without the wildcards, copy(List<T>, List<T>) would demand both lists have exactly the same element type, and copying Integers into a List<Number> would not compile. Notice that dest must already be at least as long as src, or copy throws IndexOutOfBoundsException; it sets positions rather than adding.

4The Snippet, Line by Line

Now each statement in the question, with the reason.

Java
List<? extends Number> a = new ArrayList<Integer>(); // compiles: ArrayList<Integer> fits ? extends Number
a.add(1); // error: a could be a List<Double>; only null can be added
List<? super Integer> b = new ArrayList<Number>(); // compiles: Number is a supertype of Integer
b.add(1); // compiles: every possible list here accepts an Integer
Integer x = b.get(0); // error: get returns Object for ? super Integer
Object y = b.get(0); // compiles: 1

For a.add(1), we know the actual object is an ArrayList<Integer>, but the compiler only looks at the declared type, List<? extends Number>. Another assignment could have made a a List<Double>, and adding 1 to it would corrupt it. Since the compiler cannot rule that out, it refuses. Only null, which belongs to every type, may be added.

For b.add(1), the declared type says the list holds Integer or a supertype, and an Integer can go into a List<Integer>, a List<Number> or a List<Object>. Every possibility accepts it, so it compiles.

For Integer x = b.get(0), the list could be a List<Object> holding a String, so the element might not be an Integer. The only safe type for the result is Object, which is why the next line, with Object y, compiles and prints 1 when we look at it.

5Follow-up questions

If List<? extends Number> cannot be added to, what is it for?

It is for methods that read: summing, printing, searching, copying from. Such methods become usable with any list of numbers. Read-only use is very common, which is why ? extends appears so often in good APIs.

Why does Comparator<? super T> appear in sort methods?

A comparator consumes T values, it receives two of them to compare, so it is a consumer and takes ? super T. That lets a Comparator<Number> or Comparator<Object> sort a List<Integer>.

Should return types use wildcards too?

Generally no. A wildcard in a return type pushes the uncertainty onto every caller, who then cannot add to the result and may need casts. Wildcards belong in parameters, where they make the method accept more arguments.

6Conclusion

In this answer, we saw that PECS, producer extends and consumer super, follows from asking which operations are safe for every list that might be passed. Reading from ? extends T always gives a T, and writing a T into ? super T is always safe. Collections.copy(List<? super T> dest, List<? extends T> src) applies both. In the snippet, a.add(1) fails because a might be a List<Double>, b.add(1) works because every List<? super Integer> accepts Integers, and Integer x = b.get(0) fails because the read only gives Object.

What to say in the interviewAbout 80 seconds aloud

Open with

PECS stands for producer extends, consumer super. If a parameter produces values that my method reads, I declare it with ? extends T, because whatever the real list is, every element is at least a T.

Then, if they want more

If a parameter consumes values that my method writes, I use ? super T, because whatever the real list is, it can hold a T. If I both read and write, I use the exact type.

Collections.copy shows both halves: copy(List<? super T> dest, List<? extends T> src). src is only read, dest is only written, so I can copy a List<Integer> into a List<Number>.

For the snippet: the declaration List<? extends Number> a = new ArrayList<Integer>() compiles, but a.add(1) does not, because the compiler only knows a is a list of some subtype of Number, and it could be a List<Double>, so it cannot allow adding an Integer; only null can be added.

List<? super Integer> b = new ArrayList<Number>() compiles, and b.add(1) compiles too, because any list of Integer or a supertype accepts an Integer. But Integer x = b.get(0) does not compile, because the list could be a List<Object> containing anything, so get only returns Object. Object x would be fine.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain PECS with the signature of Collections.copy, and say which of these compile:": https://scrivio-interview-library.netlify.app/java/generics/63392b1b56/