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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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         • • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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   • • Number of slices to send:
Optional 'thank-you' note:
• • Pretty simple-

Greenhorn
Posts: 8   • • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• 2
• • Number of slices to send:
Optional 'thank-you' note:
• • 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  • 