Basically, Java collection classes like Vector
, ArrayList
, HashMap
, etc. don't take primitive types, like int
.
In the olden days (pre-Java 5), you could not do this:
List myList = new ArrayList();
myList.add(10);
You would have to do this:
List myList = new ArrayList();
myList.add(new Integer(10));
This is because 10
is just an int
by itself. Integer
is a class, that wraps the int
primitive, and making a new Integer()
means you're really making an object of type Integer
. Before autoboxing came around, you could not mix Integer
and int
like you do here.
So the takeaway is:
integerBox.add(10)
and integerBox.add(new Integer(10))
will result in an Integer
being added to integerBox
, but that's only because integerBox.add(10)
transparently creates the Integer
for you. Both ways may not necessarily create the Integer
the same way, as one is explicitly being created with new Integer
, whereas autoboxing will use Integer.valueOf()
. I am going by the assumption the tutorial makes that integerBox
is some type of collection (which takes objects, and not primitives).
But in this light:
int myInt = 10;
Integer myInteger = new Integer(10);
One is a primitive, the other is an object of type Integer
.