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

# Multiple Assignments from Khalid's book

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
Code:
int[] a = {10, 20, 30, 40, 50}; // an array of int
int index = 4;
a[index] = index = 2;
In the Khalid's book it says:
a[index] = index = 2;
a[4] = index = 2;
a[4] = (index = 2); // index gets the value 2. = is right associative.
a[4] = 2; // The value of a[4] is changed from 50 to 2.

However, when I ran the program I got the following result:
a[index] = 30
Can someone explain why in the book it says the result is a[4] = 2 instead to be a[2] = 30???

author
Posts: 151
12
• Number of slices to send:
Optional 'thank-you' note:
int[] a = {10, 20, 30, 40, 50}; // an array of int
int index = 4;
a[index] = index = 2;
After the execution of this code, the value of index is 2.
What is the value of a[2], i.e. a[index]? 30. :-)
All the best.
-- khalid mughal

Ranch Hand
Posts: 3178
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Khalid A. Mughal:
int[] a = {10, 20, 30, 40, 50}; // an array of int
int index = 4;
a[index] = index = 2;
After the execution of this code, the value of index is 2.
What is the value of a[2], i.e. a[index]? 30. :-)
All the best.
-- khalid mughal

I got confused as well, Mr.Mughal. I tried to test it in a program...
What I understand is that the number 2 is assigned to index and index becomes 2. After that a[index], which is a[2], is assigned to the value of index, which is 2, isn�t it?
It�s kinda comfusion�

Ranch Hand
Posts: 341
• Number of slices to send:
Optional 'thank-you' note:
I try,

result:
0:10
1:20
2:30
3:40
4:2

Ko Ko Naing
Ranch Hand
Posts: 3178
• Number of slices to send:
Optional 'thank-you' note:
It prints 30 in the following program... What do u think?

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
What i think here is as following.....
int[] a = {10, 20, 30, 40, 50}; //1
int index = 4; //2
a[index] = index = 2; //3
System.out.println(a[index]); //4
Here at line no 3 it should be read as
a[index]=(index=2);
Now index value is 4 as from line 2 so let assigning from left
a[4]=(index=2); index value is assigned as 2 here on right.
a[4]=2;
and in line 4. System.out.println(a[2]); would execute as index value is 2 from above.
with regards
priya

Ko Ko Naing
Ranch Hand
Posts: 3178
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by priya jain:

Now index value is 4 as from line 2 so let assigning from left
a[4]=(index=2); index value is assigned as 2 here on right.
a[4]=2;
and in line 4. System.out.println(a[2]); would execute as index value is 2 from above.

So do u mean that index value assigned in (index=2) is not applied in a[index]? The index in the a[index] still use the old value 4, right?
As conclusion, index in a[index] is still 4 and index in (index=2) change the value to be 2....

Ranch Hand
Posts: 79
• Number of slices to send:
Optional 'thank-you' note:
[] has higher precedence than ().

 Don't get me started about those stupid light bulbs.