# Arrays

Tina Tibrewal
Ranch Hand
Posts: 40

Can anyone explain this?

raj malhotra
Ranch Hand
Posts: 288

step1: [3] evalutes to be as int 3
step2: a gets the reference of b array in a=b
stet3: now b[3] is calculated ie 3
step 4: that 3 is promoted as a int
step5: a[3] is calculated which is 14

Tina Tibrewal
Ranch Hand
Posts: 40
I didnt get you raj.
Why is b[3] evaluated?
When we have assigned a = b it should be a[3] which is 3 now since
a = b;
And finally a[3] should be 3 as a now points to b.

Pankaj Patel
Ranch Hand
Posts: 74
Hi. I think it is somewhat Stack like situation.
So i am trying to explain in that way.

Order of Expression Evaluated.
-------------
Stack Started.
-------------
i) a[ii]
ii)
i) [3]
ii)a=b
-------------
Before you go down from stack, first think that here in statement i) the reference of a is the array of int[] that is {11,12,13,14};
-------------
So the result is 14. And once the Execution completes, the reference of b is assingned to a.
-------------

Am i right?

Tina Tibrewal
Ranch Hand
Posts: 40
if that is the case why

Throws Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main(ABC.java:10)

anil kumar
Ranch Hand
Posts: 447
hi

i could not understand one thing here.

When we a=b
the array pointing to a may be grabage collected.
Beacuse there are no referencing pointing to the array.
But here it is not like that.
Why ?

anil kumar
Ranch Hand
Posts: 447
Hi

Tina

when we say a=b;
means we are assigning the array reference which is pointing by b ,is assigning to a
System.out.println(a[(a = c )[3]]);
the above statement can also written as
System.out.println(a[(c)[3]]);

Tina Tibrewal
Ranch Hand
Posts: 40
Thats the point anil.
Its not ebing evaluated that way.
Otherwise the original problem should have yielded a result
of 3.

Pankaj Patel
Ranch Hand
Posts: 74
Anil :
Here int[] a will not be garbase collected until System.out.println(...) completes. Because in this statement still the array a in being referenced. And once after that int[] a is elibible for garbase collection.

Tina :
Originally Posted.
[I]if that is the case why

Throws Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main(ABC.java:10)[/I]

Stack :
1.0) a[XXX] //XXX indicates Pending to BE Evaluated
2.0
2.1 [3]
2.2 (a=c)

Now Here, a[XXX] is still refer to original int[] a.
Now you go from to of the Stack.
so, First array a will refers to array c. Please note that this is on a stack only.
Then it will see for the fourth element(Index : 3) in array a. (You can say array c). Now the third element in a[] or say c[] is 40. So it will finally try to find out value of a[40]. And you already know, this is wrong index since it can point to max index 3.

I hope, i am not wrong. and making you the correct sense.Tx.

anil kumar
Ranch Hand
Posts: 447
Hi

Pankaj

Yes you are right

Tina Tibrewal
Ranch Hand
Posts: 40

1.0) a[XXX] //XXX indicates Pending to BE Evaluated
2.0
2.1 [3]
2.2 (a=c)

Now Here, a[XXX] is still refer to original int[] a.
Now you go from to of the Stack.
so, First array a will refers to array c. Please note that this is on a stack only.
Then it will see for the fourth element(Index : 3) in array a. (You can say array c). Now the third element in a[] or say c[] is 40. So it will finally try to find out value of a[40]. And you already know, this is wrong index since it can point to max index 3.
[/QB]

Hi Pankaj,

I am getting what you are trying to say but then

dont you think this should print 3 instead of 14.

I hope I am able to coney what is my problem
[ April 20, 2007: Message edited by: Tina Tibrewal ]

Pankaj Patel
Ranch Hand
Posts: 74
Try to Realize given recursion program.

The output of this program is :
-----------------
Recursion Variable : 0
Recursion Variable : 1
Recursion Variable : 2
Recursion Variable : 3
Recursion Variable : 4
Recursion Variable : 5
Recursion Variable : 6
Recursion Variable : 7
Recursion Variable : 8
Recursion Variable : 9
Recursion Variable : 10
Recursion Variable : 10
Recursion Variable : 10
Recursion Variable : 9
Recursion Variable : 8
Recursion Variable : 7
Recursion Variable : 6
Recursion Variable : 5
Recursion Variable : 4
Recursion Variable : 3
Recursion Variable : 2
Recursion Variable : 1

----------------------
Now can image what is happening here. The value increasing upto 10. That is ok. But once it starts, decresing the value is still the original one which was received.

I hope this will clear you little. Tx.
[ April 20, 2007: Message edited by: Pankaj Patel ]

jon ruane
Greenhorn
Posts: 13
in response to Tina's question:

the reason that this code compiles:

int[] a={11,12,13,14};
int[] b={0,1,2,3};
System.out.println(a[(a=b)[3]]);

and this code does not:

int[] a={11,12,13,14};
int[] b={1,2,3,4};
System.out.println(a[(a=b)[3]]);

is to do with the assignment of a=b. when you assign a value to a variable, you are returning the value that is being assigned. i.e int x=1 will return the value of 1.

in the above code, when we say: (a=b)[3], this can be read as return the value of b[3]. a=b returns the value b, and then we are looking up index [3] in b's array.

the value of b[3] is different in the two pieces of code. b[3]=3 in the code that compiles, and b[3]=4 in the code that does not. that is why you are getting the ArrayOutOfBounds exception; there is no index 4 in array 'a'. System.out.println(a[4]).

took me a while to figure this out. hope iv explained it as simple as....

Tina Tibrewal
Ranch Hand
Posts: 40
Hi jon,

But doesnt the value of a change when we do a=b;
Shouldnt a be pointing to b now.
and on doing a[3], 3 should be printed and not 14.

jon ruane
Greenhorn
Posts: 13
i think these guys explain it better:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

15.13.2 Examples: Array Access Evaluation Order

In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. For example, in the (admittedly monstrous) expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a.
Thus, the example:

class Test {
public static void main(String[] args) {
int[] a = { 11, 12, 13, 14 };
int[] b = { 0, 1, 2, 3 };
System.out.println(a[(a=b)[3]]);
}
}

prints:

14

because the monstrous expression's value is equivalent to a[b[3]] or a[3] or 14.

Srinivasan thoyyeti
Ranch Hand
Posts: 557
Hi Tena,

Hi
I haven't discovered any thing new... here... just elaborating the solution

Enjoy the day.
[ April 20, 2007: Message edited by: Srinivasan thoyyeti ]

Tina Tibrewal
Ranch Hand
Posts: 40
Hi Everyone,

Thanks for expalining this stuff to me.

Thanks & Regards,
Tina