forked from paulnguyen/code
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericJava.java
More file actions
executable file
·54 lines (46 loc) · 1.18 KB
/
GenericJava.java
File metadata and controls
executable file
·54 lines (46 loc) · 1.18 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
import java.util.* ;
class C1 {}
interface I1 {}
interface I2 {}
class noBounds<A> { }
class singleBound<A extends C1> { }
/* There seems to be a bug in GJ compiler
not recognizing implments clause...
class multipleBounds<A implements I1>{
public int foo( A obj )
{
return obj.methodA() ;
}
}
*/
class Cell<A> {
A value ;
Cell ( A v ) {
value = v ;
}
A get() { return value ; }
void set( A v ) {
value = v ;
}
}
// Run Some Misc. Test Cases
class GenericJava
{
public static void main( String[] args )
{
// all parameterized types share same run-time class
Vector<String> x = new Vector<String>() ;
Vector<Integer> y = new Vector<Integer>() ;
System.out.println( "Parameterized types share same run-time class: " +
(x.getClass() == x.getClass()) ) ; // true
// Uncheck Warnings
Cell x1 = new Cell<String>("abc") ;
System.out.println( x1.value ) ;
System.out.println( x1.get() ) ;
x1.set( "def" ) ; // Warning. Erasure changed argument type to Object
/*
GenericJava.java:42: warning: unchecked call to set(A) as a member of the raw type Cell
x1.set( "def" ) ;
*/
}
}