
arraylist的泛型转换机制
arraylist的get()方法可以将object数组中的元素直接返回为泛型类型,这与其他自定义类中需要类型转换形成了鲜明对比。下面解释了背后的机制。
在arraylist的源码中,elementdata是一个object数组,它存储着元素。get()方法直接返回elementdata[index],而无需类型转换。这是因为java泛型实现了类型擦除。
类型擦除
泛型类型在编译时被擦除,也就是说泛型信息在运行时丢失。这意味着get()方法无法在运行时知道object数组中的元素的实际类型。
实际转换
因此,实际的类型转换不是在get()方法中进行的,而是在调用get()方法的位置进行的。例如,如果将arraylist声明为arraylist
自定义类中的类型转换
在自定义类中,类型转换需要显式指定,因为jvm不知道方法返回的对象的实际类型。这是因为缺少类型擦除,泛型信息在运行时可用。
示例代码
以下代码说明了类型检查在不同场景中的不同发生位置:
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import java.lang.reflect.parameter;
public class generictypeinspection {
public static void main(string[] args) {
class> arraylistclass = arraylist.class;
method getmethod = arraylistclass.getmethod("get", int.class);
// 获取get方法的参数类型
parameter parameter = getmethod.getparameters()[0];
system.out.println("arraylist get method parameter type: " + parameter.gettype());
// (实际的类型转换发生在这里)
getmethod.invoke(new arraylist<>(), 1);
// 获取customgenericclass里的方法
class> customgenericclass = customgenericclass.class;
method getintegermethod = customgenericclass.getmethod("getinteger");
// (类型转换需要显式指定)
integer integer = (integer) getintegermethod.invoke(new customgenericclass<>());
}
private static class customgenericclass {
public t getinteger() {
return (t) new object();
}
}
} 运行这段代码,输出结果将如下所示:
arraylist get method parameter type: int
(实际的类型转换发生在这里)
CustomGenericClass里的方法 (类型转换需要显式指定)










