aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about multiple assignments" Watch "Question about multiple assignments" New topic
Author

Question about multiple assignments

Catherine Fogg
Greenhorn

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
Greenhorn

Joined: Sep 04, 2002
Posts: 18
Hi,
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.
Thanks,
Jana
Dirk Schreckmann
Sheriff

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]
 
 
subject: Question about multiple assignments