• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about multiple assignments

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to take a look at last week's conversation on the assignment operator.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic