crater
crater

Reputation:

What exactly happens while you assign a reference to an element in Array in Java?

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

Upvotes: 5

Views: 5459

Answers (5)

OscarRyz
OscarRyz

Reputation: 199363

Why in java, when I access t[0] , it returns me "a", rather than "b"?

Because you told him to hold a reference to the object with "a".

Java don't manipulate directly the objects. Instead I uses has object references. But this reference hold the value of the reference ( not the reference ) So when you assign to t[0] the value of a, they both reference the same object.

Let's see if this pic explains it better:

alt text http://img520.imageshack.us/img520/8857/referencesq4.png

First, a and t[0] reference the same object.

Later a drop that reference but t[0] no.

At the end a and t1 reference the same object and t[0] remains unchanged.

Is this because of the GC?

No

and can i believe it is safe to do such an operation

Yes

Upvotes: 0

S.Lott
S.Lott

Reputation: 392070

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.

Upvotes: 10

Jon Skeet
Jon Skeet

Reputation: 1504122

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.)

Upvotes: 2

user7094
user7094

Reputation:

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.

Upvotes: 6

Vinze
Vinze

Reputation: 2539

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.

Upvotes: 1

Related Questions