将整数添加到ArrayList时 [英] When adding an integer to ArrayList
问题描述
我在这里有一个非常愚蠢的问题.当我们将一个int值添加到ArrayList时,它将创建一个具有该int值的新Integer对象吗? 例如:
I have a very stupid question here. When we add an int value to an ArrayList, will it create a new Integer object of that int value? For example:
int a = 1;
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(a);
在上面的代码中,"a"是具有值1的原始类型,"list"是包含Integer类型元素的数组列表.因此,在将"a"添加到列表"时,列表"如何将"a"视为整数?
In the code above, 'a' is a primitive type which has value 1, 'list' is an arraylist which contains elements of Integer type. So when adding 'a' to 'list', how does 'list' treat 'a' as an Integer?
推荐答案
a
是自动装箱到Integer
.在链接中,
自动装箱是Java编译器在原始类型及其对应的对象包装器类之间进行的自动转换.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
这篇关于将整数添加到ArrayList时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!