tags:

views:

895

answers:

5

I am doing the following statements in Java,

Obj t[] = new Obj[10];

Obj a = new Obj("a");
t[0] = a;

a = new Obj("b");
t[1] = a;

Why in java, when i access t[0] , it returns me "a", rather than "b"? Is this because of the GC? and can i believe it is safe to do such an operation

+1  A: 

When you do t[0] = a; you assign the address of object you created by new Object("a") in the array, whatever you re-use the a variable won't change the value contained in the array.

Vinze
+4  A: 

The problem is, Java doesn't have pointers.

When you assign one object variable to another, it changes what that variable points to but not any other references you might have.

So:

Object a = new Object("a");
Object b = new Object("b");

a = b;
b = new Object("c");

// At this point, a = "b"
// and b = "c"

As you can see, although you first set a = b, when you then set b to be a new Object, a still retains the reference to the old object.

Phill Sacre
Which is exactly what one would expect and not a “problem”, actually. :)
Bombe
Sorry, should have made it clearer that this is expected behaviour. Design feature of Java rather than a problem as such.
Phill Sacre
Well not having pointers is NOT a Java problem.
OscarRyz
+2  A: 

The value in the array is just a reference. It's just like doing this:

Obj a = new Obj("a"); 
Obj t0 = a;
a = new Obj("b");

At the end, the t0 variable has the value it was given on line 2 - that is, a reference to the first Obj that's created on line 1. Changing the value of the a variable doesn't change the value of t0.

Once you understand the above, just think of an array as a collection of variables, e.g.

Obj[] t = new Obj[10];

is roughly like declaring:

Obj t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;

(There are plenty of differences, but in the context of this question they're similar.)

Jon Skeet
+2  A: 

Here's exactly what's happening.

Obj t[] = new Obj[10];  // 1

Obj a = new Obj("a");   // 2
t[0] = a;

a = new Obj("b");       // 3
t[1] = a;               // 4
  1. You create an array that can hold 10 references to instances of Obj. Call this obj01. You assign it to t. Note that the variable t and the actual object obj01 have a pretty casual relationship.

  2. You create an instance of Obj, call this obj02. You assign a reference to this object to the variable a. Note that the variable a and the actual object obj02 have a pretty casual relationship.

    You also put this reference into t[0]. You have one object known in two places. The object obj02 (with a value of "a") is known as a and known also as t[0].

  3. You create an instance of Obj, call this obj03. You assign a reference to this new object to the old variable a. Note that the variable a and the actual object obj03 have a pretty casual relationship. a used to reference obj02, but it doesn't reference that anymore.

    So, obj01 (an array) is referenced by t; obj02 (and instance of Obj) is known as t[0]; obj03 (an instance of Obj) is known as a.

  4. You put the reference that's in a into t[1]. So, t[1] gets a reference to obj03.

At this point all the objects are referenced by variables which are in scope. Since all objects have references, they can't be garbage collected.

S.Lott
A: 
OscarRyz