• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Arrays

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The answer is 14.
Can anyone explain this?
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if that is the case why


Throws Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main(ABC.java:10)
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

please tell me
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Pankaj

Yes you are right
 
Tina Tibrewal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jon,

Thanks for the reply.
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

Thanks for expalining this stuff to me.

Thanks & Regards,
Tina
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic