Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics what does <?> actually mean? [duplicate]

Tags:

java

generics

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.

like image 563
mezamorphic Avatar asked Jul 09 '12 09:07

mezamorphic


People also ask

What is class <?> Mean in Java?

In object-oriented programming, a class is a basic building block. It can be defined as template that describes the data and behaviour associated with the class instantiation.

What is <? Super T in Java?

super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .

What is the difference between List <? Extends T and List <? Super T >?

super is a lower bound, and extends is an upper bound.

What is E and T in Java generics?

T is meant to be a Type. E is meant to be an Element ( List<E> : a list of Elements) K is Key (in a Map<K,V> ) V is Value (as a return value or mapped value)


1 Answers

<?> 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
like image 138
Colin Hebert Avatar answered Sep 28 '22 02:09

Colin Hebert