Java Generic Methods, Bounded Types and Wildcards
Three copies of lastOf become one <T> T lastOf(List<T>). T extends Number lets largest call doubleValue and refuses a list of String. A List<? extends Number> totals seats and fees but won't take an add, and a List<? super Integer> accepts 30 and 45.
What you will learn
- Write one generic method that works for a list of any type
- Restrict a type parameter with a bound such as T extends Number
- Read from a List<? extends Number> and explain why you can't add to it
- Add values to a List<? super Integer>, and pick T, extends or super for a job
Before you start
- Java generics: type-safe containers
- Java methods and overloading
This lesson covers three things: one method that works for any list, a rule for what it accepts, and a question mark that means "some type".
One method, any list
Imagine three methods, lastName, lastScore and lastFee, for lists of String, Integer and Double. The body is the same in each: return items.get(items.size() - 1);. A generic method writes it once.
package org.studyeasy.s04l06;
import java.util.List;
public class Ex01_GenericMethod {
// <T> before the return type makes the method generic
static <T> T lastOf(List<T> items) {
return items.get(items.size() - 1);
}
public static void main(String[] args) {
List<String> students = List.of("Asha", "Ravi", "Neha");
List<Integer> scores = List.of(72, 85, 91);
List<Double> fees = List.of(49.5, 19.99, 75.0);
String lastName = lastOf(students); // T is String
Integer lastScore = lastOf(scores); // T is Integer
Double lastFee = lastOf(fees); // T is Double
System.out.println("Last name: " + lastName);
System.out.println("Last score: " + lastScore);
System.out.println("Last fee: " + lastFee);
}
}
Last name: Neha
Last score: 91
Last fee: 75.0
The <T> right before the return type is what makes the method generic. In the signature, T is a blank, and each call fills it in from the list you pass: String for students, Integer for scores, Double for fees. Integer wrong = lastOf(students); doesn't compile, because T is String there.
A bound: T must be a Number
// T must be a Number, so doubleValue() is allowed
static <T extends Number> T largest(List<T> values) {
T best = values.get(0);
for (T value : values) {
if (value.doubleValue() > best.doubleValue()) {
best = value;
}
}
return best;
}
List<Integer> seats = List.of(30, 45, 25);
List<Double> fees = List.of(49.5, 19.99, 75.0);
System.out.println("Most seats: " + largest(seats));
System.out.println("Top fee: " + largest(fees));
Most seats: 45
Top fee: 75.0
T extends Number is the bound: only number types can be T. Integer, Double and Long all qualify. Because T is always a Number, doubleValue() is allowed. The loop keeps the biggest so far. Pass a List<String> and javac refuses it with incompatible bounds: T would be String, but the upper bound is Number. The bound is a rule at the door.
? extends Number: read from it
// ? extends Number: a list of any kind of Number. Read from it.
static double total(List<? extends Number> amounts) {
double sum = 0;
for (Number amount : amounts) { // every item is a Number
sum += amount.doubleValue();
}
return sum;
}
List<Integer> seats = List.of(30, 45, 25);
List<Double> fees = List.of(49.5, 20.0, 30.25);
System.out.println("Seats: " + total(seats));
System.out.println("Fees: " + total(fees));
Seats: 100.0
Fees: 99.75
Instead of T, the parameter uses a wildcard, a question mark. List<? extends Number> is a list of some kind of Number, and we don't know which. Every item is a Number, so reading is always safe, and one method handles both lists. But amounts.add(10) inside the method doesn't compile. If the list passed in is a List<Double>, an Integer 10 would be wrong, and Java can't tell which type would be safe. The error mentions CAP#1, the compiler's name for that unknown type.
? super Integer: put values in
// ? super Integer: any list that can hold an Integer. Write to it.
static void addSeats(List<? super Integer> target) {
target.add(30);
target.add(45);
}
List<Integer> seats = new ArrayList<>();
List<Number> amounts = new ArrayList<>();
amounts.add(19.99);
addSeats(seats); // a List<Integer>
addSeats(amounts); // a List<Number> works too
System.out.println("seats: " + seats);
System.out.println("amounts: " + amounts);
seats: [30, 45]
amounts: [19.99, 30, 45]
List<? super Integer> means any list that can hold an Integer: a List of Integer, Number or Object. So adding Integers inside is safe. Both calls work, and the 19.99 stays. addSeats(new ArrayList<String>()) fails, because a String list can't hold an Integer.
Recap
| Write | Use it when |
|---|---|
<T> T lastOf(List<T>) | you want the same type back |
<T extends Number> | only some types should be allowed |
List<? extends Number> | you only read from the list |
List<? super Integer> | you put values into the list |
An AI will often pick the right one for you, but knowing these lets you read the code it writes. Generics show up again in every collection that follows, starting with sets.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)