0
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.

Edited by mike_2000_17: Fixed formatting

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by ARaza110
0

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.

Edited by coil: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.