language: Java (sun-jdk-1.7.0_10)
date: 622 days 10 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;
 
class Main
{
  static int x = 0;
 
  static List<? extends Number> foo1() {
    // we can return any List we want. YAY!
    switch(x) {
      case 0: {
        List<Integer> li=Arrays.asList(1, 2);
        return li;
      }
      case 1: {
        List<Number> ln=new ArrayList<Number>();
        ln.add(1);
        ln.add(Math.PI);
        return ln;
      }
      case 2: {
        List<Double> lf=Arrays.asList(1.1, Math.E, Math.PI);
        return lf;
      }
    }
    return null;
  }
 
  static <T> List<T> foo2() {
    // we are much more constrained here.
    // since we have no Ts, we can only return empty lists and null
    // but we return always a List<T>, where T comes from the caller
    switch(x) {
      case -1:
        return null;
      default:
        return new ArrayList<T>();
    }
  }
 
  public static void main(String[] args) {
    for(; x<3; x++) {
    List<? extends Number> wild=foo1();
    List<Integer> ints=Main.<Integer>foo2();
    List<Number> nums=Main.<Number>foo2();
 
    // we can insert into ints and nums
    ints.add(0+x);
    nums.add(42.0/x);
 
    // but not in wild
    // wild.add(1);
 
    System.out.println("x: " + x);
    System.out.println("wildcards: " + wild);
    System.out.println("ints: " + ints);
    System.out.println("nums: " + nums);
    }
  }
}