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 for coil

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.