posted 10 months ago

Evaluation order is left to right. See this part of the Java Language Specification ...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7

Henry

Anda Cristea wrote:

The expression is evaluated from right to left.

Evaluation order is left to right. See this part of the Java Language Specification ...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7

Henry

posted 10 months ago

- 1

in other words...

a = a++ + ++a;

a++ is evaluated first. it is 1111, then a is incremented to 1112.

Then ++a is evaluated, so a's value of "1112" increased to "1113", and now we have that.

now we add the first term which was 1111 to the second term, wich is 1113, which gives us 2224.

I think this is correct.

a = a++ + ++a;

a++ is evaluated first. it is 1111, then a is incremented to 1112.

Then ++a is evaluated, so a's value of "1112" increased to "1113", and now we have that.

now we add the first term which was 1111 to the second term, wich is 1113, which gives us 2224.

I think this is correct.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Fred Kleinschmidt

Bartender

Posts: 571

9

posted 10 months ago

- 1

And the above confusion is the exact reason why one should never modify a variable more than once in a single statement, although Java allow it. you should not get into the habit of doing this, since if you ever move to another language (such as C or C++) that statement will exhibit undefined behavior, sometimes giving you the answer you want (and expect), and sometimes not.

posted 10 months ago

- 1

It is super simple if you think it through simply.

1. a++ post increment. So what you do is take current value and place it into the expression and only then increment.

2. ++a pre increment. From the last operation, after value got placed into expression it got incremented, so now a is 1112. Pre increment you do similarly, but first increment, then place into expression. So a is 1112 now, you increment it becomes 1113. So

1. a is 1. First operation is pre increment. So you increment and only then place into the expression, which makes

2. a is 2. next operation is post increment. Place current value which is 2 into expression and only after that increment

3. so a is 3 now, because you placed into expression and had to increment from previous step. Now operation is pre decrement. Same, first decrement, then place into expression. Since a is 3, so it becomes 2 again

4. a is 2 again. Pre increment now, so increment first and then place into expression.

5. a is 3. So last post increment. Same technique. Place current value into expression and then increment

what happens here is, if you no longer use 'a' anywhere else, its incremented value just dissapears up in the air. And that is your final answer above. Haven't run on my machine, but I'm sure its correct

Using that simple thinking I find it difficult to get confused. There could be 50 operations, same amount of effort, thinking only about current value and looking either you need to increment/decrement first and place into expression, or first place and then do that.

Did it become clear?

1. a++ post increment. So what you do is take current value and place it into the expression and only then increment.

2. ++a pre increment. From the last operation, after value got placed into expression it got incremented, so now a is 1112. Pre increment you do similarly, but first increment, then place into expression. So a is 1112 now, you increment it becomes 1113. So

__Whatch a bit more longer:__1. a is 1. First operation is pre increment. So you increment and only then place into the expression, which makes

2. a is 2. next operation is post increment. Place current value which is 2 into expression and only after that increment

3. so a is 3 now, because you placed into expression and had to increment from previous step. Now operation is pre decrement. Same, first decrement, then place into expression. Since a is 3, so it becomes 2 again

4. a is 2 again. Pre increment now, so increment first and then place into expression.

5. a is 3. So last post increment. Same technique. Place current value into expression and then increment

what happens here is, if you no longer use 'a' anywhere else, its incremented value just dissapears up in the air. And that is your final answer above. Haven't run on my machine, but I'm sure its correct

Using that simple thinking I find it difficult to get confused. There could be 50 operations, same amount of effort, thinking only about current value and looking either you need to increment/decrement first and place into expression, or first place and then do that.

Did it become clear?