java泛型最常用的场景应该是容器类。java的泛型是伪泛型,是编译期泛型,编译的时候会进行类型检查,生成字节码的时候会进行类型擦除。
在get后会进行强制类型转换。这个是在字节码里做的操作。
写了个简单的测试代码:
[java] view plain copy
public class Test {
public static void main(String[] args) {
ArrayList list=new ArrayList();
list.add(new Date());
Date myDate=list.get(0);
}
然后反编了下字节码,如下
[java] view plain copy
public static void main(java.lang.String[]);
Code:
0: new #16 // class java/util/ArrayList
3: dup
4: invokespecial #18 // Method java/util/ArrayList."
7: astore_1
8: aload_1
9: new #19 // class java/util/Date
12: dup
13: invokespecial #21 // Method java/util/Date.""
16: invokevirtual #22 // Method java/util/ArrayList.add:(L
va/lang/Object;)
19: pop
20: aload_1
21: iconst_0
22: invokevirtual #26 // Method java/util/ArrayList.get:(I
java/lang/Object;
25: checkcast #19 // class java/util/Date
28: astore_2
29: return
附关于checkcast的解释:
checkcast checks that the top item on the operand stack (a reference to an object or array) can be cast to a given type. For example, if you write in Java:
return ((String)obj);
then the Java compiler will generate something like:
aload_1 ; push -obj- onto the stack
checkcast java/lang/String ; check its a String
areturn ; return it
checkcast is actually a shortand for writing Java code like:
if (! (obj == null || obj instanceof )) {
throw new ClassCastException();
}
// if this point is reached, then object is either null, or an instance of
// or one of its superclasses.
不能用类型参数替换基本类型。就比如,没有ArrayList,只有ArrayList。因为当类型擦除后,ArrayList的原始类型变为Object,但是Object类型不能存储double值,只能引用Double的值。