初学习,泛型疑问请教。

public class test {
public void say(ArrayList arg) {
//
}
}

public class lx {
public static void main(String[] args) {
test t1 =new test ();
ArrayList b1=new ArrayList();
b1.add("你好");
t1.say(b1); //为什么不会报错
}
}

各位,lx类中t1.say(b1)这里为什么没有报错? say方法定义了只能传递存放Integer类型的ArrayList,为什么存放字符串型的ArrayList没有报错?

你哪里定义了只能integer??你没有加泛型当然什么都能存了

你贴的代码里面 say方法只是定义了ArrayList类型,没有规定一定是存放Integer类型的ArrayList

img