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;
In
int a = 1;
ArrayList list = new ArrayList();
list.add(a);
the last line is automatically converted by the compiler into:
list.add(Integer.valueOf(a));
Integer.valueOf is a method that either creates a new Integer object with the same value, or reuses one that already exists. The resulting object has no relation to the variable a, except that it represents the same value.