What is the difference between ? and Object in Java generics?

An instance of HashMap<String, String> matches Map<String, ?> but not Map<String, Object>. Say you want to write a method that accepts maps from Strings to anything: If you would write

public void foobar(Map<String, Object> ms) {
    ...
}

you can’t supply a HashMap<String, String>. If you write

public void foobar(Map<String, ?> ms) {
    ...
}

it works!

A thing sometimes misunderstood in Java’s generics is that List<String> is not a subtype of List<Object>. (But String[] is in fact a subtype of Object[], that’s one of the reasons why generics and arrays don’t mix well. (arrays in Java are covariant, generics are not, they are invariant)).

Sample:
If you’d like to write a method that accepts Lists of InputStreams and subtypes of InputStream, you’d write

public void foobar(List<? extends InputStream> ms) {
    ...
}

By the way: Joshua Bloch’s Effective Java is an excellent resource when you’d like to understand the not so simple things in Java. (Your question above is also covered very well in the book.)

Leave a Comment