Semyon Evsei

Greenhorn

Posts: 7

posted 5 years ago

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?

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?

posted 5 years ago

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

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

posted 5 years ago

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 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

posted 5 years ago

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

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

posted 5 years ago

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

as the interpretation of the bytecode. Notice there are numbers in the output, which show which

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

- 2

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 secondMihai 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.

`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:

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

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.

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |