Course contentsShow
← Java
Lesson 949 of 2,87022. Generics: Bounds and WildcardsFree lesson

Unbounded Wildcards: The ? Type

Understanding List<?> as a list of unknown type and why it's different from raw types or Object generics.

Unbounded Wildcards: The ? Type

You'll learn what List<?> means and why it's different from raw types or List<Object>.

What Is an Unbounded Wildcard?

The unbounded wildcard ? by itself represents an unknown type. When you see List<?>, it means "a list of some specific type, but I don't know (or care) what that type is."

This is fundamentally different from:

  • Raw types (List): No type safety at all—completely bypasses generics
  • List<Object>: A list specifically typed to hold Object instances

Why Use List<?>?

Imagine you need a method that prints the size of any list, regardless of what's inside:

public void printSize(List<?> list) {
    System.out.println("Size: " + list.size());
}

This method works with List<String>, List<Integer>, List<ArrayList<Double>>—any list at all. You're saying "I accept a list of something, and I only need operations that don't depend on that something."

Key Differences

  • List<?> preserves type safety. The compiler knows a specific type exists, even if your code doesn't know what it is.
  • List (raw) has no type safety—compiler warnings everywhere.
  • List<Object> is not a supertype of List<String>—they're incompatible.

With List<?>, you can safely call methods like size(), isEmpty(), or clear(), but you cannot add elements (except null) because the compiler doesn't know what type is safe to add.

Key Takeaway: List<?> means "a list of some unknown but specific type"—it's type-safe, read-mostly, and accepts any generic list, unlike raw types or List<Object>.