How to Use the Unbounded Wildcard List<?> in Java Generics
A Java method whose parameter is a List<?> accepts a list of any element type: a List<String>, a List<Integer> or anything else. The ? is the unbounded wildcard, and it's needed because a List<Object> parameter refuses a List<String>.
Syntax
static ReturnType methodName(List<?> name) {
// read elements as Object, or use size(), isEmpty() and the like
}
| Part | What it does |
|---|---|
List<?> | A list of unknown type, so the method accepts a list of any element type. |
name | The parameter's name. Its elements are read as Object. |
ReturnType, methodName | The method's return type and name, as in any method. |
The Java Tutorials, Unbounded Wildcards: "The unbounded wildcard type is specified using the wildcard character (?), for example, List<?>. This is called a list of unknown type." It names two cases for it: "If you are writing a method that can be implemented using functionality provided in the Object class", and "When the code is using methods in the generic class that don't depend on the type parameter. For example, List.size or List.clear."
The Example
The example's describe method takes a List<?> and reports its size and its first element. main passes it a List<String> and a List<Integer>. A changed version then declares the parameter as List<Object> instead.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

import java.util.List;
public class Main {
// List<?> accepts a list of any element type.
static String describe(List<?> items) {
return items.size() + " items, the first is " + items.get(0);
}
public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");
List<Integer> limits = List.of(10, 500, 2000);
System.out.println(describe(roles));
System.out.println(describe(limits));
}
}
Step 2: Run It
java Main.java
2 items, the first is editor
3 items, the first is 10
java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.
Step 3: Use List<Object> Instead
Replace Main.java with a version whose describe takes a List<Object>:
import java.util.List;
public class Main {
static String describe(List<Object> items) {
return items.size() + " items, the first is " + items.get(0);
}
public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");
System.out.println(describe(roles));
}
}
java Main.java
Main.java:12: error: method describe in class Main cannot be applied to given types;
System.out.println(describe(roles));
^
required: List<Object>
found: List<String>
reason: argument mismatch; List<String> cannot be converted to List<Object>
1 error
error: compilation failed

What Happened
describeworked for both lists.size()doesn't depend on the element type, and+turned the first element into text, whatever its type.- With
List<Object>, the program didn't compile:List<String> cannot be converted to List<Object>. A list of strings isn't a list of objects to Java, although every string is an object. The tutorial says the same about its own example, which can't takeList<Integer>,List<String>orList<Double>"because they are not subtypes ofList<Object>".
The reason is what a method may put in. The tutorial: "You can insert an Object, or any subtype of Object, into a List<Object>. But you can only insert null into a List<?>." A List<?> parameter promises that the method won't add anything else, so any list is safe to pass.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
List<Object> to mean "a list of anything" | List<String> cannot be converted to List<Object> | Use List<?> |
Adding to a List<?>, as in items.add("x") | error: no suitable method found for add(String), with String cannot be converted to CAP#1 | Take a List<String> when the method has to add strings |
Reading an element of a List<?> as a String, as in String s = items.get(0); | error: incompatible types: CAP#1 cannot be converted to String | Read it as an Object, and check its type with instanceof |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error: method describe in class Main cannot be applied to given types;, with reason: argument mismatch; List<String> cannot be converted to List<Object> | The parameter is a List<Object> | Make it a List<?> |
error: no suitable method found for add(String) and String cannot be converted to CAP#1 | The code adds to a List<?> | Give the parameter the element type the method adds |
error: incompatible types: CAP#1 cannot be converted to String | An element of a List<?> is read as a String | Read it as an Object |
FAQ
What is CAP#1 in the error?
The compiler's name for the unknown type behind one ?. The error explains it: where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?. All the compiler knows about CAP#1 is that it's some kind of Object.
Can I add anything to a List<?>?
Only null, as the tutorial says. Adding null to a list of strings through a List<?> parameter compiled, and the list printed [a, null].
Conclusion
List<?> lets a method accept a list of any element type, as long as it reads the elements as Object and uses only methods that don't depend on the type, such as size(). List<Object> isn't the same thing: it accepts only a List<Object>.
Additional Resources
- Unbounded Wildcards and Guidelines for Wildcard Use, in The Java Tutorials.
