• 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

JQ+ Assignment Precedence Question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK here it is:

This is a little tricky... the answer is:
30 20 30
Any good explanations? Is this exam likely to try one like this?
Thanks
Zac
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could some one also point out a good source to understand "Left to Right" and "Right to Left" associativity.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,
JLS 15.7 Evaluation Order
HIH
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Zac Roberts:
OK here it is:

This is a little tricky... the answer is:
30 20 30
Any good explanations? Is this exam likely to try one like this?
Thanks
Zac


Simply put, the trick is in the following line:

The first thing that is done when this line is executed is that each of the operands are evaluated. This is done in a left to right manner. Therefore iA[i], then i is evaluated, and then 30 is evaluated. Of course, there isn't much evaluating to do in order to evaluate 30.
Now, once all of the operands have been evaluated, the assignments take place (from right to left). 30 is assigned to i and the return value (30) is then assigned to iA[i], which was evaluated prior to the assignment to i, so this is really iA[0].
Hence, the correct answer is 30 20 30.
Corey
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's the chart you want...
http://www.csc.calpoly.edu/~csc101/studynotes/JavaOperators.htm
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin - I forgot to consult the JLS first . Anyways, let me check it out.
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zac!
Let me take a shot at this:-
If you look at operator precedence "[]" has the highest precedence and "=" has the lowest precedence.
Hence the expression:
iA[i] is evaluated first,
Hence the expression now becomes:-
iA[0] = i = 30
Associativity of "=" operator is from right to left. Therefore, the statments are evaluated in the following order:-
1. i = 30 which returns 30
2. iA[0] = 30
and then obviously, iA[1] = 20.
I hope this answers your question.
 
Zac Roberts
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good thanks...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic