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 Beginning Java and the fly likes Question about multiple assignments 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 » Java » Beginning Java
Bookmark "Question about multiple assignments" Watch "Question about multiple assignments" New topic

Question about multiple assignments

Catherine Fogg

Joined: Jul 19, 2002
Posts: 1
Could anyone enlighten me about the behaviour of multiple assignments? Given the following code:
int i = 0;
int[] a = { 3, 6 };
a[i] = i = 9;
I would have expected that the last line would evaluate as follows:
(a[i] = (i = 9))
a[i] = 9
a[9] = 9
= ArrayIndexOutOfBoundsException
However, it evaluates to:
a[0] = 9
Can anyone explain?
Eureka Jana

Joined: Sep 04, 2002
Posts: 18
Yah what you expected is correct, but it is not like that.See the operators precedence.
Higher :[]
Lower: =
So first a[i] gets evaluated and the assignment from right to left.
I would like to convey one message to all.Since we are all beginners, the basic thing in learning any language is to make sure we are well aware of "Operators Precedence".That solves many problems.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
You may want to take a look at last week's conversation on the assignment operator.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Question about multiple assignments
Similar Threads
SoDuko puzzle
assignment order
Question on Precedence/Assignment
Confusion with operator '=' right associativity
Regular expression