• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What is the result of j += j-- ?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I came accross the following example;



I would have thought that the result would be 21. However the result is 20. So what happens to ++?

if I have the following example:



Then the result is 21, however i = 2 so this time round the ++ is not ignored! Why in the first case ++ was ignored then?

 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From the Coderanch FAQs....

https://coderanch.com/how-to/java/PostIncrementOperatorAndAssignment

Not exactly the same expression, but the same reason for the effect.

Henry
 
Marshal
Posts: 75668
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because there are two values involved. There is the value of i and that of i++. The two are different. Read the FAQ Henry referred to, and ask again if you still don*#x2019;t understand the problem.
 
Ranch Hand
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty simple-


 
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Semyon Evsei wrote:Hello, I came accross the following example;



I would have thought that the result would be 21. However the result is 20. So what happens to ++?





Let's rewrite that expression to see it better: j = j + j++;
Now, the order is this one:
1) We sum up j + j, those in red.
2) Now the green stuff happen so j++ which means our j is now 11.
3) But, after this expression has been evaluated our j (blue) get the value of the evaluated expression which is the one in red that we calculated in the first step. So j = 20.
That value we had in red is practically stored until we finish evaluating our little expression.
 
Semyon Evsei
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
From the Coderanch FAQs....

https://coderanch.com/how-to/java/PostIncrementOperatorAndAssignment

Not exactly the same expression, but the same reason for the effect.

Henry



Thank you for the link. I now (*think*) I understand. So j is incremented but this new value of j is afterwards overwritten by the value of the evaluated expression.
In other words, if there was another j in that sum (after the j++) then I would have to consider the new value of j.

Thanks
 
Campbell Ritchie
Marshal
Posts: 75668
354
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mihai Boisteanu wrote:. . . j = j + j++;
Now, the order is this one:
1) We sum up j + j, those in red.
2) Now the green stuff happen so j++ which means our j is now 11.
3) But, after this expression has been evaluated our j (blue) get the value of the evaluated expression which is the one in red that we calculated in the first step. So j = 20.
That value we had in red is practically stored until we finish evaluating our little expression.

No, that is incorrect. The ++ operator has a higher precedence than +, so it must be executed before the addition +. But an operator with a higher precedence has a narrower scope, so only the second j is in the scope of the operator ++. The second j is not in the scope of the + operator, but the expression j++ is in its scope. Remember the value of j++ is the same as the (old) value of j.

What happens is this:
  • The value of j is put onto a stack.
  • The value of j is put onto a stack.
  • The value of j is incremented by 1, but this value is not put onto the stack.
  • The two values on the stack are added.
  • The stack looks like this:
    That is how the old value of j is used for j++. You can work it out by creating a methodNow using the instruction javap -c Foo, you get something like
    iload_1
    iload_1
    iinc
    iadd

    as the interpretation of the bytecode. Notice there are numbers in the output, which show which int is being incremented, and by how much, e.g. 1, 1, meaning the first int incremented by 1.
    If you use i += i++; instead, the bytecode looks exactly the same.
    Putting the iload and stack together, we getIf you search this forum you will find many threads about this question; it seems to confuse everybody at some stage in their learning. If you try i += ++i; it is much easier to understand, and in the bytecode the iinc comes before the second iload.
     
    Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic