Generics what does <?> actually mean? [duplicate]
<?>
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:
- Effective java - Generics.pdf
- Java language specification - Type Arguments and Wildcards
- oracle.com - Java tutorials, wildcards
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.
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.
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.