---
title: "Java的Generics的几点限制"
author: "standalone"
date: "2012-12-28T15:00:52Z"
tags: []
categories: ["编程语言"]
slug: "java-generics"
draft: true
lastmod: "2013-06-01T16:21:34Z"
id: 1756416
url: "https://standalone.iteye.com/blog/1756416"
---

参见

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


<span style="color:red">Cannot Instantiate Generic Types with Primitive Types</span>

这个是说你不能用primitive type来做类型参数，就是说下面这个使用泛型的例子是不对的：

```java

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

```



<span style="color:red">Cannot Create Instances of Type Parameters</span>
这个是不能直接new T()来创建类型参数的实例。下面的例子里，在run time，有关T或者E的类型信息都找不到了，所以你必须用反射的技术来做到：

```java

T instantiateElementType(List<T> arg)
{
  return new T(); //causes a compilation error
}

public static <E> void append(List<E> list, Class<E> cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}

```

这里反射的技术就是传一个类作为参数。


<span style="color:red">Cannot Declare Static Fields Whose Types are Type Parameters</span>

这个容易引起混乱的，看下面这个例子：
如果类的静态变量允许是类型参数：

```java
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禁止这样做。

<span style="color:red">Cannot Use Casts or instanceof with Parameterized Types</span>
因为java在编译是做了type erasure，所以无法区分ArrayList<Integer>和ArrayList<String>，所以Java不允许如下code编译通过：

```java
public static <E> void rtti(List<E> list) {
    if (list instanceof ArrayList<Integer>) {  // compile-time error
        // ...
    }
}
```


<span style="color:red">Cannot Create Arrays of Parameterized Types</span>
也就是说下面这行code会编译错误。

List<Integer> arrayOfLists = new List<Integer>;  // compile error

为什么要禁止这个呢？先看不同类型的object插入到array里会怎么样：


```java

Object[] strings = new String[2];
strings[0] = "hi";   // OK
strings[1] = 100;    // An ArrayStoreException is thrown.

```


换成generic list呢，假设下面第一行code能够编译通过，那么第三行code在运行时就无法抛出ArrayStoreException了。所以Java要禁掉这样写法。


```java
Object[] stringLists = new List<String>[];  // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList<String>();   // OK
stringLists[1] = new ArrayList<Integer>();  // An ArrayStoreException should be thrown,
                                            // but the runtime can't detect it.
```


<span style="color:red">Cannot Create, Catch, or Throw Objects of Parameterized Types</span>
一个generic的class不能继承Throwable（间接或直接都不行）， 不能catch类型参数的实例（但可以throw）。例如：


```java
// Extends Throwable indirectly
class MathException<T> extends Exception { /* ... */ }    // compile-time error

// Extends Throwable directly
class QueueFullException<T> extends Throwable { /* ... */ // compile-time error


public static <T extends Exception, J> void execute(List<J> jobs) {
    try {
        for (J job : jobs)
            // ...
    } catch (T e) {   // compile-time error
        // ...
    }
}
```

这些都会编译错误。

<span style="color:red">Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type</span>
这个是说因为type erasure的原因，会导致像

```java
public class Example {
    public void print(Set<String> strSet) { }
    public void print(Set<Integer> intSet) { }
}
```

两个print方法完全一样，所以也被禁止。

以上是java文档上列出来的，还有一种情况：

```java
 public List<? extends Foo> getFoos()
  {
    List<? extends Foo> foos = new ArrayList<? extends Foo>();
    foos.add(new SubFoo());
    return foos;
  }
```

第三行会编译错误“Cannot instantiate the type ArrayList<? extends Foo>", 原因是如果这行ok，那么编译器就不知道是否add一个SubFoo是安全的，如果foos被赋值成ArrayList<AltFoo> 呢？记住，compiler要在编译期解决所有类型检查的，所以这些能引起混乱的它肯定要禁掉了。参见[stackoverflow上面的回答](http://stackoverflow.com/questions/176446/how-can-elements-be-added-to-a-wildcard-generic-collection)。
多说一点的是更详细的关于genercis的使用，参见
<http://tutorials.jenkov.com/java-generics/wildcards.html>，
关于? extends A的解释是List<? extends A>是只知道是一个list，可以装A或者A的子类，所以你可以读取里面的内容并cast成A， 但是不能insert。