Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# Multiple Assignments from Khalid's book

Greenhorn
Posts: 2
• • • • 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 = index = 2;
a = (index = 2); // index gets the value 2. = is right associative.
a = 2; // The value of a 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 = 2 instead to be a = 30???

author
Posts: 116
7
• • • • 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, i.e. a[index]? 30. :-)
All the best.
-- khalid mughal

Ranch Hand
Posts: 3178
• • • • 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, 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, is assigned to the value of index, which is 2, isn�t it?
It�s kinda comfusion� Ranch Hand
Posts: 341
• • • • I try,

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

Ko Ko Naing
Ranch Hand
Posts: 3178
• • • • It prints 30 in the following program... What do u think? Greenhorn
Posts: 10
• • • • 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=(index=2); index value is assigned as 2 here on right.
a=2;
and in line 4. System.out.println(a); would execute as index value is 2 from above.
with regards
priya

Ko Ko Naing
Ranch Hand
Posts: 3178
• • • • Originally posted by priya jain:

Now index value is 4 as from line 2 so let assigning from left
a=(index=2); index value is assigned as 2 here on right.
a=2;
and in line 4. System.out.println(a); 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
• • • • [] has higher precedence than (). Stop it! You're embarassing me! And you are embarrassing this tiny ad! the value of filler advertising in 2020 https://coderanch.com/t/730886/filler-advertising