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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# From deepak's mock

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
Hi
This is from deepak's mock
Consider the following code :

1. int [ ] a = new int[ 2 ];
2. int b = 1;
3. a[b] = b = 0;

Value will be stored in what element of array a.
A.a[1]
B.none
C.a[0]
How can it be a[1].I didn't get this.Can any of you please explain.
Regards
Sudha

Ranch Hand
Posts: 73
• Number of slices to send:
Optional 'thank-you' note:
You should know the evaluation order of an expression.
this order is from left to right.
1. int [ ] a = new int[ 2 ];
2. int b = 1;
3. a[b] = b = 0;
at line 3,first a[b] is evaluated.now b is 1,so a[b] is a[1].
now you can see it's a[1] that is being assighned a value.
hope this may clear your doubt.
Bin Zhao

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
[] gets precedence over =

Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
This is the same example explained in RHE chapter 2.
The game is all about order of evaluatig expression and operator precedence and associativity.
In this example value will be stored in a[1] and that to 0.
final expression will become a[1] = 0;
How ?
Beacause in java all expressions are evaluated first from left to right.
so after this we will have
a -> a[1]
b -> b (just a reference)
0 -> 0 (literal)
so now expression becomes
a[1] = b = 0;
now associativity of oprators comes in to play.
= operator is evaluated right to left
so expression reduces to
a[1] = b (where now b = 0)
so finaly 0 is assigned to a[1]

Originally posted by sudha:
[B]Hi
This is from deepak's mock
Consider the following code :

1. int [ ] a = new int[ 2 ];
2. int b = 1;
3. a[b] = b = 0;

Value will be stored in what element of array a.
A.a[1]
B.none
C.a[0]
How can it be a[1].I didn't get this.Can any of you please explain.
Regards
Sudha

Greenhorn
Posts: 28
• Number of slices to send:
Optional 'thank-you' note:
That was the wonderful explanation Rajesh. Where did u get it. Actually I want to delve a bit more into evaluation of expressions.
I knew the funda but couldn't get it documented anywhere.
Hope to hear from u soon.

Leverager of our synergies
Posts: 10065
• Number of slices to send:
Optional 'thank-you' note: