-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayOfGeneric.java
More file actions
47 lines (35 loc) · 1.03 KB
/
ArrayOfGeneric.java
File metadata and controls
47 lines (35 loc) · 1.03 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
package generics;
import java.util.*;
/**
* RUN:
* javac generics/ArrayOfGeneric.java && java generics.ArrayOfGeneric
* OUTPUT:
* Generic[]
*/
public class ArrayOfGeneric {
static final int SIZE = 100;
static Generic<Integer>[] gia;
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// Exception java.lang.ClassCastException:
//
// [Ljava.lang.Object; cannot be cast to [Lgenerics.Generic;
//
// gia = (Generic<Integer>[]) new Object[SIZE];
gia = (Generic<Integer>[]) new Generic[SIZE];
System.out.println(gia.getClass().getSimpleName());
gia[0] = new Generic<Integer>();
// incompatible types:
//
// required: Generic<Integer>
// found: Object
//
// gia[1] = new Object();
// incompatible types^
//
// required: Generic<Integer>
// found: Generic<Double>
//
// gia[2] = new Generic<Double>();
}
}