Adding an Object to Vector loses Reference using Java?

Posted by thechiman on Stack Overflow See other posts from Stack Overflow or by thechiman
Published on 2010-03-07T15:14:58Z Indexed on 2010/03/09 2:06 UTC
Read the original article Hit count: 309

Filed under:
|
|

I have a Vector that holds a number of objects. My code uses a loop to add objects to the Vector depending on certain conditions. My question is, when I add the object to the Vector, is the original object reference added to the vector or does the Vector make a new instance of the object and adds that?

For example, in the following code:

private Vector numbersToCalculate;
StringBuffer temp = new StringBuffer();

while(currentBuffer.length() > i) {
    //Some other code
    numbersToCalculate.add(temp);
    temp.setLength(0); //resets the temp StringBuffer
}

What I'm doing is adding the "temp" StringBuffer to the numbersToCalculate Vector. Should I be creating a new StringBuffer within the loop and adding that or will this code work? Thanks for the help!

Eric

© Stack Overflow or respective owner

Related posts about java

Related posts about stringbuffer