我在这里有一个非常愚蠢的问题。当我们将一个int值添加到ArrayList时,它将创建一个具有该int值的新Integer对象吗?
例如:
int a = 1;
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(a);
在上面的代码中,“ a”是具有值1的原始类型,“ list”是包含Integer类型元素的数组列表。因此,在将“ a”添加到“列表”时,“列表”如何将“ a”视为整数?
最佳答案
a
是autoboxed到Integer
。通过链接,
自动装箱是Java编译器在原始类型及其对应的对象包装器类之间进行的自动转换。