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 question about OperandOrder 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 "question about OperandOrder" Watch "question about OperandOrder" New topic
Author

question about OperandOrder

michael wang
Ranch Hand

Joined: Feb 06, 2002
Posts: 35
public class OperandOrder {
public static void main(String args[]) {
int i=0;
int a[] = {3,6};
a[i] = i = 9;
System.out.println(i + " " + a[0] + " " + a[1]);
}
}

the code above prints
9 9 6
how can i get it?


--<br />a java beginner from China
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
First evaluate the array variable as [] operator has the highest precedence. This will give you:
a[0] = i = 9;
The associativity of = operator is from right to left. So i will be assigned 9 and a[0] will be assigned 9 after that.
This justifies what is printed.
Brian
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: question about OperandOrder
 
Similar Threads
Assignment resolution
question from KAM
a[i]=i=9
Confusion with operator '=' right associativity
Why doesn't this match?