• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Assignments from Khalid's book

 
oliveira leitao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Khalid A. Mughal
author
Ranch Hand
Posts: 115
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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�
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try,

result:
0:10
1:20
2:30
3:40
4:2
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prints 30 in the following program... What do u think?
 
priya jain
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[] has higher precedence than ().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic