In principle, Java collection classes such as Vector , ArrayList , HashMap , etc., do not accept primitive types, such as int .
In the old days (pre-Java 5), ββyou could not do this:
List myList = new ArrayList(); myList.add(10);
You would need to do this:
List myList = new ArrayList(); myList.add(new Integer(10));
This is because 10 is just an int . Integer is a class that wraps an int primitive, and creating new Integer() means that you are actually making an object of type Integer . Before you start auto-boxing, you cannot mix Integer and int , as you are here.
So, takeaway:
integerBox.add(10) and integerBox.add(new Integer(10)) will add Integer to integerBox , but only because integerBox.add(10) transparently creates Integer for you. Both methods may not necessarily create an Integer in the same way as explicitly created using new Integer , while autoboxing will use Integer.valueOf() . I assume that the tutorial states that integerBox is some type of collection (which accepts objects, not primitives).
But in this light:
int myInt = 10; Integer myInteger = new Integer(10);
One of them is primitive, the other is an object of type Integer .
source share