-5
class TestEntity {
    public int x;
    public int y;

    public TestEntity(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public String toString() {
        return super.toString() + ", x -> " + x + ", y -> " + y;
    }
}

TestEntity t = new TestEntity(666, 777);
List<TestEntity> list = new ArrayList<>();
list.add(t);
t = null;
System.out.println(list.get(0));

why correct print list.get(0) with

@xxxxx,x -> 666, y -> 777

if I remove t = null; and do t.x = 888, the print looks correct.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
Li Shen
  • 35
  • 6
  • 2
    can you be a bit more clear in your question? – Stultuske Sep 01 '17 at 08:10
  • `t = null;` just dereference the variable `t`, but doesn't change anything on the `TestEntity` object that it referenced previously. You should learn/search about "java references" – Joel Sep 01 '17 at 08:13
  • Can you define the result you hae, and what you expected? – Turtle Sep 01 '17 at 08:13
  • you cahnge the reference of t when yyou do t=null, that doesnt mean the TestEntity in the list is modified thoe – ΦXocę 웃 Пepeúpa ツ Sep 01 '17 at 08:14
  • Java is pass-by-value https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. This is a duplicate – Jack Flamp Sep 01 '17 at 08:14
  • What logical should be applied here to think that `t=null` would affect the object added to the list? You __obviously__ don't add `t` to the list, but rather the reference to an object currently hold by `t`. If you alter `t` to refer to something else (a new object or `null`) then it obviously doesn't matter much to `list`. – Tom Sep 01 '17 at 08:15

2 Answers2

0

When you add t in list, t and the first element of list refer to the same object.

But as you write t = null, it assigns null to the t variable.
So, now t and the first element of list doesn't refer any longer to the same object.
So printing the first or the second will give distinct result.

As you write t.x = 888, t and the first element of list refer still to the same object.
You change only the value of a field of the object.
So printing the first one or the second give exactly the same result.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

You're not setting an object to null. You're setting a reference to an object to null.

t is a reference to an object. When calling list.add(t), you're storing a copy of this reference (let's call it t2) inside the list. So you end up with

t -----> theObject
           ^
           |           
ArrayList [t2]

Two references both pointing at the same object.

Now, when you execute t = null, you just change what t points to. The copy, stored in the list, doesn't care about that. Only t now points to no object at all (null). So you end up with

t--> nothing    theObject
                   ^
                   |           
        ArrayList [t2]

So, printing the first element of the list still prints the object that is referenced by t2, the first reference in the list internal array.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255