Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about OperandOrder

 
michael wang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic