Vector v = new Vector();
i<10;
v.add(new Integer(i));// what does this lien do?

I dont understand, what does calling (new Integer()) do? Is is calling the constructor
and passing the value i in it.

Why can't we just pass the value i directly and add it in the vector.

Member Avatar

The Integer class is a wrapper class for an int . While an int is a primitive (has no methods, etc.), an Integer is a class (does have objects).

The constructor for an Integer class requires an int, which is why you see the constructor call new Integer(i). Vectors require Integers, therefore you need to make the int into an Integer.

Thank you all for your replies.