What is the difference between ? and Object in Java generics?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
In Java generics, ? and Object are related but not equivalent. Object is a concrete type, while ? means unknown captured type. This difference controls assignment compatibility, which operations are allowed, and how you should design APIs.
Concrete Type Versus Unknown Type
List<Object> means the list element type is exactly Object. List<?> means the list element type is some specific type, but that type is unknown at the current use site.
That unknown type could be String, Integer, or any reference type, but compiler rules preserve type safety.
The second assignment is rejected because Java generics are invariant.
Why Invariance Matters
If List<String> were assignable to List<Object>, code could add an integer into a string list.
That would break type guarantees for all code using strings. Java prevents this at compile time.
Read and Write Behavior
With List<Object>, you can add any object type because all reference types are subtypes of Object.
With List<?>, you can read elements as Object, but you cannot safely add non-null values.
Why null is allowed: null fits any reference type.
API Design Use Cases
Use List<?> when your method only needs to inspect data, not mutate element type-specific content.
This accepts lists of many element types without requiring callers to copy data into List<Object>.
If your method needs to insert heterogeneous values, then List<Object> may be correct.
Bounded Wildcards and PECS
Unbounded wildcard is only one part of variance tools. Bounded wildcards provide more expressive contracts.
Read from producer:
Write to consumer:
PECS rule helps remember intent:
- Producer extends.
- Consumer super.
This is usually better than forcing everything to Object.
Generic Methods Versus Wildcards
Sometimes a named type parameter is clearer than wildcard usage.
If method logic depends on one consistent element type across inputs and outputs, prefer explicit type parameter. Use wildcards mainly for flexible input acceptance.
Common Refactoring Mistakes
A common anti-pattern is replacing wildcard parameters with Object because it seems simpler. That usually reduces API compatibility and increases casts.
Another mistake is returning wildcard-heavy collection types from public APIs. Consumers then struggle to use returned values ergonomically.
Good rule:
- Input parameters can use wildcards for flexibility.
- Return types should be concrete generic types when possible.
Common Pitfalls
- Expecting
List<String>to be assignable toList<Object>. - Trying to add typed values into
List<?>directly. - Replacing wildcard parameters with
Objectand reducing API usability. - Misapplying bounded wildcards by ignoring producer-versus-consumer intent.
- Returning wildcard collections where concrete generic return types are clearer.
Summary
- '
Objectis a concrete type, while?represents unknown captured type.' - '
List<Object>andList<?>have different assignment and mutation rules.' - Invariance prevents unsafe cross-assignment between typed lists.
- Wildcards improve parameter flexibility while preserving type safety.
- Use bounded wildcards and generic methods intentionally for clean API design.
Related reading
- What is the difference between Builder Design pattern and Factory Design pattern?
- What is the difference between ''E'', ''T'', and ''?'' for Java generics?
- What is the difference between Factory and Strategy patterns?
- What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?
- What is the difference between a HashMap and a TreeMap?
- What is the difference between a JavaBean and a POJO?
- What is the difference between List of T and Collectionof T?
- What is the difference between public, private, and protected inheritance?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.