Skip to content

Instantly share code, notes, and snippets.

@archie
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save archie/9784771 to your computer and use it in GitHub Desktop.

Select an option

Save archie/9784771 to your computer and use it in GitHub Desktop.
Exploring wildcard types, or the so called question mark type, in Java.
public void process3(java.util.List<?>);
Code:
0: aload_1
1: invokeinterface #2, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
6: astore_2
7: aload_2
8: invokeinterface #3, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z
13: ifeq 31
16: aload_2
17: invokeinterface #4, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
22: astore_3
23: aload_3
24: invokevirtual #5 // Method java/lang/Object.toString:()Ljava/lang/String;
27: pop
28: goto 7
31: return
package wild;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Viewer {
public void process1(List<Object> items) {
for (Object i : items)
i.toString();
}
public void process2(List<Item> items) {
for (Item i : items)
i.process();
}
public void process3(List<?> items) {
for (Object i : items)
i.toString();
}
public void process4(List<? extends Item> items) {
for (Item i : items)
i.process();
}
public static void main(String[] args) {
Viewer viewer = new Viewer();
viewer.process3(new ArrayList<TwitterItem>(Arrays.asList(new TwitterItem() {
@Override
public void process() {
// some computation
}
@Override
public String toString() {
return "twitteritem";
}
})));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment