泛型
<>发音为typeof ArrayList<Integer> ArrayList typeof Integer
ArrayList成为原始类型
1.泛型是给编译器看的,为了规范数据类型。以前的集合可以添加各种类型的对象。
2.通过字节码比较 collection3.getClass() == collection2.getClass()
可以发现,编译过后的字节码中并没有类型信息。
3.可以通过反射,透过泛型往集合中添加各种类型元素
collection3.getClass().getMethod("add",Object.class).invoke(collection3,"abc");
4.参数化类型不考虑类型参数的继承
Vector<String> v= new Vector<Object>() //错误
Vector<Object> v= new Vector<String>() //错误
5.在创建数组实例时,数组的元素不能使用参数化的类型
Vector<Integer> vectorList[] = new Vector<Integer>[10];
? 通配符。 用于通配任意类型的集合
public static void printCollection(Collection<?> collection){
collection.size();
//collection.add(); 可以调用与参数化无关的方法
}