-
Notifications
You must be signed in to change notification settings - Fork 2
/
Wildcard.java
39 lines (27 loc) · 1.11 KB
/
Wildcard.java
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
/*
This program tests the Wildcard operator (?) for generic classes.
It is used to receive any type, restrict to superclasses, and to make variables read-only
*/
import java.util.List;
import java.util.ArrayList;
public class Wildcard {
public static void main(String args[]) {
ArrayList<Integer> readOnlyList = new ArrayList<>();
readOnlyList.add(1);
readOnlyList.add(3);
Wildcard.method(readOnlyList);
// Thing<?,? extends Number> thing = new Thing<String,String>(new String("cosa"), new String("nnn")); // DOESN'T WORK, SECOND TYPE SHOULD BE NUMBER OR SUBTYPE
Thing<?,? extends Number> thing = new Thing<String,Integer>(new String("cosa"), new Integer(8));
System.out.println("DATA: "+thing.data + " NUMBER: "+thing.number);
// thing.data = "datos"; thing.number = 11111; // DOES NOT WORK AS ? MAKES IT READ-ONLY
}
static void method(List<?> readOnly) {
System.out.println("RO: " + readOnly);
// readOnly.add(new Integer(333)); // DOES NOT WORK AS ? MAKES IT READ-ONLY
}
}
class Thing<E,F> {
public Thing(E data, F number) { this.data = data; this.number = number; }
E data;
F number;
}