-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCast.java
40 lines (32 loc) · 897 Bytes
/
Cast.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
class Cast {
static Object f() {
return "Hello, world!";
}
static String g() {
return (String) new Object();
}
static class Inner<T> {
T field;
Inner(T field) {
this.field = field;
}
}
public static void main(String[] args) {
System.out.println("begin");
String s = (String) f();
System.out.println(s);
try { g(); }
catch (ClassCastException e) {
System.out.println("catch g");
}
Inner<Integer> i = new Inner<Integer>(new Integer(42));
System.out.println(i.field);
System.out.println((Integer) i.field);
try {
System.out.println((String) (Object) i.field);
} catch (ClassCastException e) {
System.out.println("catch field");
}
System.out.println((Integer) 42);
}
}