What's does Enumeration<?>
mean?
Is there any way to represent the general generic?
The <?>
syntax is java's way to specifying that the generic type is "unbounded" - ie it can be "anything".
Enumeration<?>
is an Enumeration with an "unbounded type" - you can assign an Enumeration of any type to such a variable, for example:
Vector<String> v = new Vector<String>();
Enumeration<String> es = v.elements();
Enumeration<?> e = es; // This assignment compiles OK
However, being unbounded, the nextElement() method of an Enumeration<?>
with return type Object
(even if it's actually an Enumeration<String>
), so you'll have to cast if you want typed elements:
String s = (String)e.nextElement(); // Unsafe cast - compiler warning
The question mark indicates that it stands for all types. Here's an example:
In addition to concrete instantiation there so-called wildcard instantiations . They do not have concrete types as type arguments, but so-called wildcards . A wildcard is a syntactic construct with a " ? " that denotes not just one type, but a family of types. In its simplest form a wildcard is just a question mark and stands for "all types".
Example (of a wildcard parameterized type):
public void printPair( Pair<?,?> pair) { System.out.println("("+pair.getFirst()+","+pair.getSecond()+")"); } Pair<?,?> limit = new Pair<String,Long> ("maximum",1024L); printPair(limit);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With