Quick revision
The short answer
Both <T extends Number> double sum(List<T> xs) and double sum(List<? extends Number> xs) let us call Number methods such as doubleValue() on each element, and both accept a List<Integer> or List<Double>. When the type appears only once in the signature, the wildcard version is simpler, which is the Effective Java guideline. We need a type parameter when the same type must link several places, such as a parameter and the return type in <T extends Number> T first(List<T> xs), or when we must put values taken from the list back into it.
What to remember
In this answer, we compared a bounded type parameter with a bounded wildcard. Both let the body call Number methods and accept lists of any Number subtype, so for sum, where the type appears once, the wildcard is simpler. A type parameter is needed when the same type links several places, such as a parameter and the return type, and it also lets the body add values of type T taken from the list, while the wildcard form can only add null. The Effective Java guideline sums it up: a type parameter that appears only once should be a wildcard.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Both <T extends Number> double sum(List<T> xs) and double sum(List<? extends Number> xs) let us call Number methods such as doubleValue() on each element, and both accept a List<Integer> or List<Double>. When the type appears only once in the signature, the wildcard version is simpler, which is the Effective Java guideline. We need a type parameter when the same type must link several places, such as a parameter and the return type in <T extends Number> T first(List<T> xs), or when we must put values taken from the list back into it.
Why this question is asked
Bounds are what make generic code useful: without them, a type parameter is just Object, and we can call nothing interesting on it. But Java gives us two ways to express a bound, a bounded type parameter and a bounded wildcard, and developers often pick one at random. Interviewers ask this to see whether we know what each form lets the method body do and when the extra name of a type parameter is actually needed. In this answer, we will recap what a bound does, compare the two sum methods, look at the cases where only a type parameter works, cover what each form allows us to add to the list, and finish with the guideline for choosing.
1A Quick Recap of Bounds
An upper bound says a type must be a given type or a subtype of it. In <T extends Number>, T can be Integer, Double, BigDecimal or any other Number. In List<? extends Number>, the list's element type is some unknown subtype of Number.
The bound tells the compiler what members are available. Because every Number has doubleValue(), intValue() and so on, the method body can call them on elements whose type is T or ? extends Number. Without the bound, elements would only be Objects, and only Object's methods would be available.
2The Two sum Methods Side by Side
Here are both versions. They compile to almost the same thing and accept the same arguments.
Notice that the body is the same in both. Each element is at least a Number, so doubleValue() is available. The difference is that the first version gives the unknown element type a name, T, while the second leaves it anonymous. Since T is used nowhere else in sumT's signature, the name buys us nothing; it is only extra syntax for the reader to understand. That is why the wildcard version is the better choice for sum.
3When We Need the Type Parameter
A type parameter earns its name when the same type has to connect two or more places. The most common case is a parameter and the return type.
| Need | <T extends Number> ... List<T> | List<? extends Number> |
|---|---|---|
| Call Number methods on elements | yes | yes |
Accept List<Integer> and List<Double> | yes | yes |
| Return the caller's exact element type | yes: returns T | no: only Number |
| Link two parameters to one type | yes | no |
| Add an element read from the list | yes: it is a T | no: only null |
| Simplest signature when used once | no | yes |
With the type parameter, the caller who passes a List<Integer> gets an Integer back, because T links the list's element type to the return type. With the wildcard, the method can only promise a Number, and the caller has to cast. The same applies when two parameters must agree, as in <T extends Number> boolean same(List<T> a, T b), where b must be of the list's element type, or when the method must write back into the list values it took from it, which we look at next.
4What Each Form Lets Us Add to the List
Neither form lets us add an arbitrary Number. The caller's list might be a List<Integer>, and adding a Double to it would corrupt it, so the compiler forbids it in both versions. With the wildcard, the element type has no name, so there is nothing we can add except null. With the type parameter, there is a little more freedom: values of type T, which we can only obtain from the list itself or from a parameter of type T, can be added.
Notice that the type parameter version can move values around within the list because it knows they all share one type, T. The wildcard version cannot, because each read gives only a Number, and the compiler cannot prove that Number is the list's real element type. When a public method needs that kind of write but we want the simpler wildcard signature, we keep the wildcard publicly and call a private generic helper, a technique called wildcard capture.
5The Guideline for Choosing
Effective Java gives a simple rule: if a type parameter appears only once in a method declaration, replace it with a wildcard. A type parameter that appears once connects nothing, so the wildcard says the same thing more plainly. If it appears two or more times, in parameters, the return type or both, it is doing real work and should stay.
Applied to our examples, sum takes List<? extends Number>, first takes <T extends Number> T first(List<T> xs), and duplicateFirst keeps its type parameter because the body needs the name. Following the rule makes signatures easier to read and tells the reader, at a glance, whether types are linked.
6Follow-up questions
Is there any run-time difference between the two forms?
No. Both erase to List, and the bound erases sumT's T to Number, so the compiled methods are nearly identical. The difference is entirely in what the compiler allows callers and the body to do.
Can a wildcard have a lower bound and a type parameter cannot?
Yes. ? super Integer is allowed for wildcards, but a type parameter can only have upper bounds with extends. Lower bounds are what we use for consumers, such as a list we want to add Integers to.
Why does sumW accept List.of(1, 2.5)?
The compiler infers the list's element type as a common supertype of Integer and Double, something like Number & Comparable<...>, which is a subtype of Number, so the list matches List<? extends Number>.
7Conclusion
In this answer, we compared a bounded type parameter with a bounded wildcard. Both let the body call Number methods and accept lists of any Number subtype, so for sum, where the type appears once, the wildcard is simpler. A type parameter is needed when the same type links several places, such as a parameter and the return type, and it also lets the body add values of type T taken from the list, while the wildcard form can only add null. The Effective Java guideline sums it up: a type parameter that appears only once should be a wildcard.
Open with
Both versions let me call Number methods like doubleValue on the elements, because the bound guarantees every element is a Number, and both accept a List<Integer> or a List<Double>.
Then, if they want more
For sum, where the type is only used once, the wildcard version, double sum(List<? extends Number> xs), is simpler, and that is the Effective Java guideline: if a type parameter appears only once in the signature, replace it with a wildcard. I need a real type parameter when the same type has to link several places.
The classic case is the return type: <T extends Number> T first(List<T> xs) returns an Integer when I pass a List<Integer>, whereas the wildcard version can only return Number and the caller has to cast. Linking two parameters, like a list and a value that must be of its element type, is another case.
On writing to the list: neither version can add an arbitrary Number, because the list might be a List<Integer>. With the wildcard I can only add null. With the type parameter I can add values of type T, for example an element I read from the same list, because the compiler knows it has the list's element type.
If I need that in a public method with a wildcard signature, I use a private generic helper to capture the wildcard.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 how bounds affect what you can do inside a generic method, using <T extends...": https://scrivio-interview-library.netlify.app/java/generics/4e7f37f400/