-
Notifications
You must be signed in to change notification settings - Fork 7
/
NonGenDemo.java
60 lines (45 loc) · 1.35 KB
/
NonGenDemo.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package chapter14;
//NonGen is functionally equivalent to Gen
//but does not use generics
class NonGen {
Object ob; //ob is now of type Object
//Pass the constructor a reference to
//an object of type Object
NonGen(Object o) {
ob = o;
}
//Return type object
Object getOb() {
return ob;
}
//Show type of ob.
void showType() {
System.out.println("Type of ob is " + ob.getClass().getName());
}
}
//Demonstrate the non generic class
class NonGenDemo {
public static void main(String[] args) {
NonGen iOb;
//Create a NonGen object and store
// an integer in it. Autoboxing still occurs
iOb = new NonGen(88);
//Show the type of date used by iOb
iOb.showType();
//Get the value of iOb
int v = (Integer) iOb.getOb();
System.out.println("value : " + v);
System.out.println();
//Create another NonGen Object and
//store a String in it.
NonGen strOb = new NonGen("Non Generics Test");
// Show the type of date used by strOb
strOb.showType();
//Get the value of strOb
//Again the noticed that the cast is necessary.
String str = (String) strOb.getOb();
//This compiles but conceptually wrong
iOb=strOb;
v=(Integer)iOb.getOb();
}
}