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 list = new ArrayList();
list.add(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?
解决方案
The a is autoboxed to an Integer. From the link,
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.