This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multiple Assignments from Khalid's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multiple Assignments from Khalid Watch "Multiple Assignments from Khalid New topic
Author

Multiple Assignments from Khalid's book

oliveira leitao
Greenhorn

Joined: Dec 16, 2003
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[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

Joined: Jul 30, 2002
Posts: 103
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

Joined: Jun 08, 2002
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[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�


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
I try,

result:
0:10
1:20
2:30
3:40
4:2
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
It prints 30 in the following program... What do u think?
priya jain
Greenhorn

Joined: Apr 23, 2003
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[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

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Dec 03, 2003
Posts: 79
[] has higher precedence than ().


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Assignments from Khalid's book
 
Similar Threads
BinarySearch() Question
operand evaluation order
Multiple Assignments
Math.round() returns what ?
Array value