Forum

Meaning Of in Java ...
 
Notifications
Clear all

Meaning Of in Java Script Programming

   RSS

0
Topic starter

It's a wildcard. <?> is the same as <? extends Object>
- any type.
The way wildcards are usually used is <? extends Foo> (any class that extends Foo) or <? super Foo>
(any class that is a supertype of Foo)
Here is an example of usage of <?>:
Consider the following method, printList:
public static void printList(List<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}
The goal of printList is to print a list of any type, but it
fails to achieve that goal — it prints only a list of
Object instances; it cannot print List<Integer>,
List<String>, List<Double>, and so on, because they
are not subtypes of List<Object>. To write a generic
printList method, use List<?>:
public static void printList(List<?> list) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}
Because for any concrete type A, List<A> is a subtype
of List<?>, you can use printList to print a list of any
type:
List<Integer> li = Arrays.asList(1, 2, 3);
List<String> ls = Arrays.asList("one", "two",
"three");
printList(li);
printList(ls);

Mr nice guy

Topic Tags