-2

let's consider the following code:

public class Test {

    private int a;

    public Test(){
        this.a = 1;
    }

    public static void main(String[] args) {
        Test A = new Test();
        Test B = A; 

        // TEST1
        B.a = 0;
        System.out.println(A); // prints 0

        // TEST2
        B = null;
        System.out.println(A); //  also prints 0
    }

    public String toString(){
        return Integer.toString(this.a);
    }

}

In TEST1, we modify B and then A is modified because B points to A. I may understand why in TEST2 A doesn't become null, because B now points to null.

But the results of the two tests seem contradictory: why A doesn't become null ?

Thanks.

intermet
  • 89
  • 1
  • 6
  • 2
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Murat Karagöz May 11 '17 at 11:24
  • Well... because you didn't set A to null (only B)... – Maurice Perry May 11 '17 at 11:24
  • Instead of B = null, you could also write B = new Test(). Would you now expect A to also point to this new object? – Tobi May 11 '17 at 11:27
  • I strongly recommend that you try to understand the difference between pointers and references, for example explained here: https://softwareengineering.stackexchange.com/a/141838 – Frank Haubenreisser May 11 '17 at 11:34
  • References in Java _are_ pointers. There is no difference, @Frank Haubenreisser. "... reference values (often just references) are pointers ..." JLS §4.3.1. – Lew Bloch May 11 '17 at 17:22
  • Java's definition of "reference" differs from the C++ definition. The reason Java's references don't behave as the OP expected is that they are actually pointers. – Lew Bloch May 11 '17 at 17:43

4 Answers4

2

The variables A and B were both referencing the same object. B.a = 0 is altering that object. But when you reassign B = null, you're not altering the object. You're just altering the variable.

khelwood
  • 55,782
  • 14
  • 81
  • 108
0
Test A = new Test();

The variable A points to an instance of type Test

Test B = A;

The variable B points to the same instance as variable A does.

B.a = 0;

The instance, variable B is pointing to (the same instance variable A points to), is changed.

B = null;

Variable B does not point to any instance anymore, while variable A still points to the same instance of type Test as before.

The main thing to understand is that variables A and B do hold a pointer to an instance of type Test. The instance of type Test is represented somewhere in memory and many variables may reference (point) to it.

The expression:

B.a = 0;

means: find the instance B is pointing at and modify its state in memory.

The expression:

B = null;

means: let B point to some other instance in memory (in your case to no instance at all). While the expression:

B = A;

means: let B point to the same instance in memory as A does point to.

Harmlezz
  • 7,972
  • 27
  • 35
0

By making object b null, you just cut the referance between the test object which resides in the heap and the B. The other referance between the test object and the A remains regardless of the other referances of the object unless you cut it or changed it. regards

0

In Java variables are stored in the stack, while objects themselves are stored in the heap. Primitive type values are also stored in the stack, thus primitive type variables don't touch heap at all. But object variables on the stack contain heap addresses instead of values themselves. So by assigning null to B you change the B's (and only B's) value on the stack, but the heap value remains unchanged, and A's value on the stack remains unchanged, so A still references the same object in the heap allowing you to access its attributes and methods.

Gyrotank
  • 153
  • 1
  • 2
  • 9