-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCastNull.java
44 lines (34 loc) · 915 Bytes
/
CastNull.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
40
41
42
43
44
class CastNull {
static class InnerOne {
}
static interface InnerInterOne {
public Object objectify();
}
static class InnerTwo implements InnerInterOne {
public Object objectify() {
return this;
}
}
static class InnerThree implements InnerInterOne {
public Object objectify() {
return null;
}
}
public static void main(String[] args) {
InnerTwo t = null;
System.out.println(t == null);
t = new InnerTwo();
InnerInterOne in = (InnerInterOne)t.objectify();
System.out.println(in == null);
in = (InnerInterOne) new InnerThree().objectify();
System.out.println(in == null);
InnerOne ino = (InnerOne) new InnerThree().objectify();
System.out.println(ino == null);
try {
Object o = new InnerTwo();
InnerOne o1 = (InnerOne) new InnerTwo().objectify();
} catch (ClassCastException e) {
System.out.println("caught exception");
}
}
}