• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Semyon Evsei
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

From the Coderanch FAQs....

http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment

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

Henry
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • 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.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty simple-


 
Mihai Boisteanu
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Henry Wong wrote:
From the Coderanch FAQs....

http://www.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: 56562
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!