开发者

When I re initialise a vector why does it keep its values

开发者 https://www.devze.com 2023-04-13 09:31 出处:网络
In below class I am adding a String to a Vector(v1) and then adding that Vector(v1) to a new Vector(v2). I then re-initialise Vector v1.

In below class I am adding a String to a Vector(v1) and then adding that Vector(v1) to a new Vector(v2). I then re-initialise Vector v1.

How does java retain the reference to v1 ? When I re-initalise v1 is java maintaining a reference under the hood ?

The output of below is "1".

public class VectorTest {

    public static void main(String args[]){
        new VectorTest().testVector();
    }

    private void testVector(){

        Vector v1 = new Vector();
        Vector v2 开发者_如何学运维= new Vector();

        v1.add("1");
        v2.add(v1);
        v1 = new Vector();
        Vector v3 = (Vector)v2.get(0);
        System.out.println(v3.elementAt(0));
    }

}


private void testVector(){

    Vector v1 = new Vector(); // v1 references some vector created on the heap
    Vector v2 = new Vector(); 

    v1.add("1"); // vector referenced by v1 gets a "1" added
    v2.add(v1); // make v2[0] the second referrer to that Vector you created

    v1 = new Vector(); // Change what v1 refers to, this is not the same as destroying the original Vector
    // v2[0] still holds a reference to the original vector

    Vector v3 = (Vector)v2.get(0); // v3 is now another referrer to the same vector
    System.out.println(v3.elementAt(0));
}

You're changing what v1 refers to, in this case some other Vector. But v2 still holds a reference to the original Vector.

Objects in Java can have multiple variables referring to it. Vector objects are also mutable, meaning that if one of its referrers modifies it, like you do with:

v1.add("1");

Then every other referrer will now reference that same vector that has a "1" in it.

You can also change what objects a variable refers to, like you do here:

v1 = new Vector();

So after that happens, v1 holds a different reference to a different Vector object, while v2[0] (excuse the array notation) still holds a reference to the original Vector you created and assigned to v1 at the beginning.


Vector v3 = (Vector)v2.get(0);

When you do this, you're making v3 refer to the same Vector that you created at the beginning, so there are two referrers to this object at this point:

  1. v2[0]
  2. v3

I'd try to draw some crude images to demonstrate this, but I think it's easier just to read this page from the Java tutorial about objects and references:

http://download.oracle.com/javase/tutorial/java/javaOO/objectcreation.html


When you do:

v1 = new Vector();

What happens is that v1 is now pointing to another object, but the old vector is also referenced by v2 (since you've added it).

Java doesn't release the object until all references to it are eliminated (roughly speaking).


Use v1.clear() if you want v2.get(0) to be reinitialized, although it makes little sense here. As birryree commented, you change the reference of v1, the object itself is not influenced by the new Vector() assignment.


I'll annotate your code:

public class VectorTest {

    public static void main(String args[]){
        new VectorTest().testVector();
    }

    private void testVector(){

        Vector v1 = new Vector();  // Create a new vector as v1
        Vector v2 = new Vector();  // Create a new vector as v2

        v1.add("1");  // Add the string "1" as an element in v1
        v2.add(v1);   // Add whatever is inside v1 to v2.
        v1 = new Vector();  // Dispose old v1, get a new vector as v1
                            // THIS HAS NO EFFECT ON v2

        Vector v3 = (Vector)v2.get(0);  // Get the 0th item from v2 as a new vector v3.


        System.out.println(v3.elementAt(0));
    }

As I noted, disposing of v1 (when you get a new Vector for it) does not affect what you did with v2.

}
0

精彩评论

暂无评论...
验证码 换一张
取 消