• 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

Assignment and ++

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code fragment:

When this code is run, it produces the output:

I expected variable i to contain value 3 when it's value was printed out, but (for my surprise) value 0 has been showed. I thought this code was to work the following way:
  • In line 6, i receives 0 (original value)
  • then i is incremented by the unary operator ++.
  • In line 7, i receives the new value (1)
  • and then i is incremented by the unary operator ++.
  • and so on... until i holds the value 3, which does not happen in the real case, as we have seen.


  • The program does not behave as I expected. Could someone tell me the right behaviour of this code?
    Thanks in advance.
    Bruno.
     
    Ranch Hand
    Posts: 1865
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's a question from my mock exam that demonstrates how the postfix operator works in this situation.

    The answer is b. The explanation is as follows.


    The statement contains an addition operation. The left operand is a postfix operation and it is evaluated first. The result of the postfix operation is zero, but variable i is incremented as a side effect. The right hand operand of the addition operation is the result of method m. As a side effect, method m prints the current value of variable i which is one. Method m then returns the value zero. The result of the addition operation is zero and that is the value that is assigned to variable i.

     
    Ranch Hand
    Posts: 867
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Bruno
    It is a tricky question.

    Within the assignment from i++ to i,the i++ become i and the effect cancel because it is the postfix opperator
    The effect will take place after the syntax ';' in this case
    so this action can not take effect

    Hope this help
     
    Ranch Hand
    Posts: 3271
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This gets discussed a lot here. Check out this thread.
    Corey
     
    Bruno Arantes
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for all who helped! Such "mysteries" of java are now certainly less "mysterous" for me...
     
    Well THAT's new! Comfort me, reliable tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic