-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerics.java
More file actions
73 lines (63 loc) · 1.96 KB
/
Generics.java
File metadata and controls
73 lines (63 loc) · 1.96 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.*;
public class Generics {
// if this method is named print, it will be compile error
public static void print1(Collection<? extends List> c) {
for( List o : c ) {
System.out.println(o);
}
}
// if this method is named print or print1, it will be compile error
// this method is not callable, as no instance of extactly Collection<List> can be created
public static void print2(Collection<List> c) {
for( List o : c ) {
System.out.println(o);
}
}
public static <T> void print3(Collection<T> c) {
for( T o : c ) {
System.out.println(o);
}
}
// this is invalid
// public static <T extends List> void print4(Collection<T extends List> c) {
// this is valid, but print1 is more preferrable. See JLS p53.
public static <T extends List> void print4(Collection<T> c) {
for( T o : c ) {
System.out.println(o);
}
}
// this method is not callable, as no instance of extactly Collection<? super List> can be created
public static void print5(Collection<? super List> c) {
// for( List o : c ) { // invalid
for( Object o : c ) {
System.out.println(o);
}
}
public static void print(Collection<?> c) {
for( Object o : c ) {
System.out.println(o);
}
}
public static void main(String[] args) {
List<Object> l = new ArrayList<Object>(); // valid
// List<List<Object>> c = new ArrayList<ArrayList<Object>>(); // invalid
List<ArrayList<Object>> c = new ArrayList<ArrayList<Object>>(); // valid
print(c);
print1(c);
// print2(c); // invalid
print3(c);
print4(c);
// print5(c); // invalid
// Outer.Inner<Double> x = null; // invalid
// Outer<Integer>.Inner x = null; // invalid
Outer<Integer>.Inner<Double> x = null;
Outer.Inner y = null;
// Outer.Inner z = new Outer.Inner(); // Invalid
// Outer.Inner z = new Outer<Integer>.Inner<Double>(); // unchecked compiled error
Outer<Integer>.Inner<Double> z = new Outer<Integer>.Inner<Double>();
z.s = 0.0;
}
}
class Outer<T> {
class Inner<S> { S s; }
}