http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
To use Java generics effectively, you must consider the following restrictions:
- Cannot Instantiate Generic Types with Primitive Types
- Cannot Create Instances of Type Parameters
- Cannot Declare Static Fields Whose Types are Type Parameters
- Cannot Use Casts or instanceof With Parameterized Types
- Cannot Create Arrays of Parameterized Types
- Cannot Create, Catch, or Throw Objects of Parameterized Types
- Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
Cannot Instantiate Generic Types with Primitive Types
这个是说你不能用primitive type来做类型参数,就是说下面这个使用泛型的例子是不对的:
- class Pair<K, V> {
- private K key;
- private V value;
- public Pair(K key, V value) {
- this.key = key;
- this.value = value;
- }
- // ...
- }
- Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
// ...
}
Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error
Cannot Create Instances of Type Parameters
这个是不能直接new T()来创建类型参数的实例。你必须用反射的技术来做到:
- public static <E> void append(List<E> list, Class<E> cls) throws Exception {
- E elem = cls.newInstance(); // OK
- list.add(elem);
- }
public static <E> void append(List<E> list, Class<E> cls) throws Exception {
E elem = cls.newInstance(); // OK
list.add(elem);
}
这里反射的技术就是传一个类作为参数。
Cannot Declare Static Fields Whose Types are Type Parameters
这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
- public class MobileDevice<T> {
- private static T os;
- // ...
- }
- MobileDevice<Smartphone> phone = new MobileDevice<>();
- MobileDevice<Pager> pager = new MobileDevice<>();
- MobileDevice<TabletPC> pc = new MobileDevice<>();
public class MobileDevice<T> {
private static T os;
// ...
}
MobileDevice<Smartphone> phone = new MobileDevice<>();
MobileDevice<Pager> pager = new MobileDevice<>();
MobileDevice<TabletPC> pc = new MobileDevice<>();
那么os的实际类型是什么呢?乱套了肯定。所以Java禁止这样做。
Cannot Use Casts or instanceof with Parameterized Types
因为java在编译是做了type erasure,所以无法区分ArrayList<Integer>和ArrayList<String>,所以Java不允许如下code编译通过:
- public static <E> void rtti(List<E> list) {
- if (list instanceof ArrayList<Integer>) { // compile-time error
- // ...
- }
- }
public static <E> void rtti(List<E> list) {
if (list instanceof ArrayList<Integer>) { // compile-time error
// ...
}
}