Generics what does <?> actually mean?

JavaGenerics

Java Problem Overview


> Possible Duplicate:
> What does List<?> mean in java generics?
> What does the question mark in Java generics' type parameter mean?

Apologies but it was difficult trying to search for <?>.

What does mean in regards to Java generics? I understand <A extends B> and <A super B>, but I have never seen this question mark on its own before.

Java Solutions


Solution 1 - Java

<?> is a shorthand for <? extends Object>, it's also known as an unbounded wildcard. So you can specify any type of object in your generic.

For example the List class is declared as List<?>, because it can be a list of anything you want.


Resources:

Solution 2 - Java

Its a wildcard type. It is short for ? extends Object

If you get it, all you know is its an Object. If you try to set, you can't because it could be any sub class of Object.

Solution 3 - Java

The wildcard Generic is "something". It will be handled as something that extends from Object.

From Java documentation:

> In generic code, the question mark (?), called the wildcard, > represents an unknown type. The wildcard can be used in a variety of > situations: as the type of a parameter, field, or local variable; > sometimes as a return type (though it is better programming practice > to be more specific). The wildcard is never used as a type argument > for a generic method invocation, a generic class instance creation, or > a supertype.

So, for instance, a List<?> is a list containing objects from unknown type.

Solution 4 - Java

Its a wildcard. Suppose that you have a collection but the type of that collection is not known, thus you denote it by "?". It simply specifies that the type is not known. For more details refer http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf I'm sure it will help.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmezamorphicView Question on Stackoverflow
Solution 1 - JavaColin HebertView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaFrancisco SpaethView Answer on Stackoverflow
Solution 4 - JavapraxmonView Answer on Stackoverflow