-9

Explain how the value of val changes from 5 to 6 for v1 and v2 changes the instance

class Value {
    int val;
}

class Test {
    static void main(String[] args) {
        int i1 = 3;
        int i2 = i1;
        i2 = 4;
        System.out.print("i1==" + i1);// 3
        System.out.println(" but i2==" + i2);// 4
        Value v1 = new Value();
        v1.val = 5;
        Value v2 = v1;
        v2.val = 6;// how val changes from 5 to 6
        System.out.print("v1.val==" + v1.val);// 6
        System.out.println(" and v2.val==" + v2.val);// 6
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269

2 Answers2

2

When you do Value v2 = v1 you are creating a reference v2 to v1. Any changes you make will go back to where v1 is stored in memory. In effect, you've just created 2 names for 1 object.

jhobbie
  • 1,016
  • 9
  • 18
2

The reason that val has changed is because in Java, "objects" are essentially pointers that point to that object in memory. In your code, you define a NEW object (and a pointer called v1 that points to it) for a Value. You then specify that you want the value to be 5. When you go to create you second value object, you instead are creating another POINTER, and having that pointer ALSO point at your first object. Therefore when you change the value that your pointer is pointing at, you change the object itself. Since both pointers point at this object, it appears that both are the same.

What you should do instead is:

    Value v1 = new Value();
    v1.val = 5;
    Value v2 = new Value();
    v2.val = v1.val;
    v2.val = 6;// how val changes from 5 to 6

This creates a pointer pointing to a NEW object of the type Value, then sets the value of that object to the other objects value. This way when you reference them, they remain separate as they point to different spots in memory.

Here is an image explanation:

The left side shows the path you are taking, where you make a pointer go to the same blob, so both references edit the same blob. The right hand shows my answers, where you make a reference to a new blob in memory, and then edit that new blob. Please excuse my crude paint drawing, I'm no artist.

grg
  • 5,023
  • 3
  • 34
  • 50
Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
  • can you explain diagrammatically – user3757756 Jun 19 '14 at 19:28
  • A link to an image explanation is here: http://imgur.com/eKJAc71 The left side shows the path you are taking, where you make a pointer go to the same blob, so both references edit the same blob. The right hand shows my answers, where you make a reference to a new blob in memory, and then edit that new blob. Please excuse my crude paint drawing, I'm no artist. I've updated my answer to include this. Let me know if this clears things up, or if I can help to clear it up any more – Mike Elofson Jun 19 '14 at 19:34
  • it is not like v2 is refrence to v1 & v1 is pointing to object – user3757756 Jun 19 '14 at 19:43
  • v1 and v2 are both references. The `new Value()` is the portion that makes an object in memory. Say that this object is in memory at address 0000000A. So, when you say `Value v1 = new Value()`, you are saying that v1 references whatever is at 0000000A. Then you create another reference, and say `v2 = v1`, but v1 is a reference to the object in memory. So now v2 also points to 0000000A. So you have v1->0000000A and v2->0000000A. When you say `v2.val = 6`, it is saying `Take what is at the location referenced by v2 (0000000A) and set the value of it to 6.` – Mike Elofson Jun 19 '14 at 19:59
  • You want to instead create a new object in memory with `Value v2 = new Value()`, then you can reference this NEW object with v2. – Mike Elofson Jun 19 '14 at 20:01
  • thanx mike now i understand properly – user3757756 Jun 19 '14 at 20:03
  • Glad that I can help. Feel free to mark my answer as the answer to your question, and upvote my answer. It will make it visible if others come along with a question similar to yours. – Mike Elofson Jun 19 '14 at 20:08