• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does one best understand x++ and ++x in this code?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example:


and



and




With the first example, I am confused about how (x++ + ++x) works and it's mainly the ++x that is the really confusing me here. So if int x = '1' then x++ takes the current value of x which is '1' and stores it. So now x++ is '1' but something happens to it, but this doesn't actually change the value of 'x++' it increments thereafter to '2' but the value of 'x++' is still '1'. So when ++x runs it takes the current value (reading from left to right) and now the current value of 'x' is '2' but 'x++' ≠ '2', it's like '2' is off in the ether somewhere, and that is what is confusing me. So after all that then ++x takes what the value of 'x' is off in the ether which is '2' and increments by '1' to get 3. Altogether we have the value of '1' for 'x++' and the value of '3' for ++x so then it's 1 + 3 = 4.

With the second example, reading left to right the '++x' acts first, so it takes the current value of 'x' which is '1' and increments by '1' to get '2' then when x++ runs it stores the current value of 'x' which is '2' but does it not increment it? because when I go to print I get: 4 so that's 2 + 2.

With the last example, it's again just confusion with how x++ is functioning. I see 'x++' here as it takes the current value of 'x' which is '1' and just stores it, but similar to the last example, why doesn't it increment to '2'? It just prints: '1'


The only conclusion I can draw here is that the only time 'x++' actually increments is when it's the first operator and when it's followed by either --x, ++x in other cases of it not being first it just simply stores the current value and doesn't increment.

Please help
Thank you
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like crappy code from an exam revision book and there is no “best” way to understand it. The ++x bit is easy to understand; it is the x++ bit that is hard.

Each of those two subexpressions has two values: the values of x and the value of the whole expression. In the case of postfix incrementing, the Java® Language Specification (=JLS) tells you this:-
the JLS wrote:. . . the value 1 is added to the value of the variable and the sum is stored back into the variable.
Now that is the value of x. But the value of the whole expression is:-
the JLS wrote:The value of the postfix increment expression is the value of the variable before the new value is stored.
So those two values may be different. In fact, they are always different. The prefix increment expressions also have two values, but since
the JLS wrote:The value of the prefix increment expression is the value of the variable after the new value is stored.
the two values are the same.

You can work out the two assignments like this:-You may get the impression that my arithmetic is incorrect; I did not wish to give you the correct answer all at once. Put the right figures into such a diagram and remember the final result is the sum of the expressions not the different values of x.

We have an FAQ because so many people ask the same question.
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How I calculate this in my head, you can find it in this particular post.
Maybe it will work for you too.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . So now x++ is '1' but something happens to it, but this doesn't actually change the value of 'x++' it increments thereafter to '2' but the value of 'x++' is still '1'. . . .
No, the value of x++ which you identified correctly does not change subsequently. It is the value of x which you cannot see directly which has changed.

The only conclusion I can draw here is that the only time 'x++' actually increments is when it's the first operator and when it's followed by either --x, ++x in other cases of it not being first it just simply stores the current value and doesn't increment.

Please help
Thank you
No, that is not correct.The value of i in that code is increased to 124 but the value of i++ is still 123. You can't see i directly but you can see i++ like this (crappy code indeed!)Edit: remove an incorrect ++ sign and spelling correction.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Justin Robbins wrote:. . . So now x++ is '1' but something happens to it, but this doesn't actually change the value of 'x++' it increments thereafter to '2' but the value of 'x++' is still '1'. . . .
No, the value of x++ which you identified correctly does not change subsequently. It is the value of x which you cannot see directly which has changed.

The only conclusion I can draw here is that the only time 'x++' actually increments is when it's the first operator and when it's followed by either --x, ++x in other cases of it not being first it just simply stores the current value and doesn't increment.

Please help
Thank you
No, that is not correct.The value of i in that code is increased to 124 but the value of i++ is still 123. You can't see i directly but you can see i++ like this (crappy code indeed!)Edit: remove an incorrect ++ sign and spelling correction.


Thanks for all the information!

I don't understand how

will output 124

But


Is it like only after the operation is carried out that it can being fully incremented. So for x++ to be fully carried out it has to go through two steps before the incrementing is achieved?

So maybe I should always think of x++ as after the fact. Like for x++ will always hold the currect value but if it gets used once more then that's when it will actually get incremented?

I think I uderstand it. It's whenever x++ is acted upon more than once. But x++ will still retain it's value but will be incremented when it's used later down the line.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this will further clarify. This is how you can imagine these operators being implemented:

If you were to perform then the postIncrement() method would increment the value of i, but the program would output the old value.


If instead you performed you would be ignoring the return value of postIncrement(), and you're just printing the new value.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:The only conclusion I can draw here is that the only time 'x++' actually increments is when it's the first operator and when it's followed by either --x, ++x in other cases of it not being first it just simply stores the current value and doesn't increment.

I'm not sure how you came up with that convoluted explanation, because your original thinking from your first post was spot on. I suspect you got distracted by the "increment" bit and assumed that the new value somehow had to be used.
Not true.

Unfortunately, this is precisely what these sorts of questions are designed to do: distract you with smoke and mirrors when what you were doing originally - reading from left to right and working values out along the way - is much closer to what you should be doing.

HIH

Winston
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Maybe this will further clarify. This is how you can imagine these operators being implemented:

If you were to perform then the postIncrement() method would increment the value of i, but the program would output the old value.


If instead you performed you would be ignoring the return value of postIncrement(), and you're just printing the new value.


I am very new to java I don't know what "this." means yet.

But I do think I understand what's happening with x++ and ++x.

It's only when x++ is directly acted upon is when it changes its increment. So x++ for all intents and purposes will always store the CURRENT value of 'x' but if asked upon once more (directly using it), then it will increment. But only if it's being directly used and not indirectly.

The two cases I think about are:



Here it will print out '4' because even though x++ could increment again it doesn't because it's not being directly acted upon. And so ++x takes in '1' and increments to '2' then x++ takes on the current value of '2' then stores it, but since nothing is asking it to do anything else it simple stores that value.

Whereas,


The x++ takes the value of '1' and stores it, but since ++x is asking for the current value then it's being DIRECTLY acted upon so it must complete it's operation to increment to '2' even though technically x++ = '1' still. So ++x takes on '2' and increments it to '3'. Then when they are added together it's 1 + 3 = 4.

And finally,


the x++ simply stores the value of 1, but then it's DIRECTLY acted upon and asked for its value once more so this forces it's incrementing so then when it prints, it will print to '2'

Please identify any inconsistencies in the way I am thinking about these. I must understand it.

Thank you
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4 months ago I came up with this explanation, which I find easy to follow.

Let's simulate such example:

x = 3
y = (x++) + (++x) + (x++)
y = ?

Think only about one operation at a time:
x++ place to expression, then increment
++x increment, then place to expression

First line
1. x = 3

Second line
x++ means, place value to the expression, then increment. Your x = 3 from the first line.
2. y = 3 + (++x) + (x++)

Remember, you placed 3 to the expression, but haven't incremented yet, so your x after increment becomes 4. And beside that you have next operation ++x, which means, increment first, then place to expression. So x = 4 + 1.
3. y = 3 + 5 + (x++)

So, next operation you have x++, which means again, place x value to the expression, and only then increment. Your x is 5.
4. y = 3 + 5 + 5

5. y = 13

Technically you still need to increment x after you placed it to the expression, but it doesn't make any difference in your case anymore because sum has been assigned to y already. If you were to use x later on in further calculations, your x would be 6.

Hope it is clear now.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:It's only when x++ is directly acted upon is when it changes its increment. So x++ for all intents and purposes will always store the CURRENT value of 'x' but if asked upon once more (directly using it), then it will increment.

As Winston already said, I'm afraid this is not true. Both ++x and x++ immediately assign the value x+1 to x. After you evaluate either of these two expressions, the value of x in memory will have incremented. However, both of these are expressions, and expressions return a value after they've been evaluated, much like the expression 6+2 returns the value 8 when you evaluate it.

Think of them as sending your family a picture of your house after renovation. ++house renovates your house, and sends your family a picture of it after it's done. house++ takes a picture of your old house, renovates it and then sends the picture. Even though in the latter case your family gets a picture of the old house, in both cases the house has been renovated.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:4 months ago I came up with this explanation, which I find easy to follow.

Let's simulate such example:

x = 3
y = (x++) + (++x) + (x++)
y = ?

Think only about one operation at a time:
x++ place to expression, then increment
++x increment, then place to expression

First line
1. x = 3

Second line
x++ means, place value to the expression, then increment. Your x = 3 from the first line.
2. y = 3 + (++x) + (x++)

Remember, you placed 3 to the expression, but haven't incremented yet, so your x after increment becomes 4. And beside that you have next operation ++x, which means, increment first, then place to expression. So x = 4 + 1.
3. y = 3 + 5 + (x++)

So, next operation you have x++, which means again, place x value to the expression, and only then increment. Your x is 5.
4. y = 3 + 5 + 5

5. y = 13

Technically you still need to increment x after you placed it to the expression, but it doesn't make any difference in your case anymore because sum has been assigned to y already. If you were to use x later on in further calculations, your x would be 6.

Hope it is clear now.



Couldn't what I said have worked on



Because x=3 then (working from left to right) x++ stores '3' then the ++x to the right acts upon x++ so x++ increments to '4' but x++ is still '3' so now ++x takes on '4' and increments once more to '5' then x++ simply stores the current value of where 'x' is at and since nothing else is calling upon that last x++ then it stops there.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working left to right (the value of x after the increment is in []'s):

and x is 6 at the end.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Working left to right (the value of x after the increment is in []'s):

and x is 6 at the end.


Thanks Dave!

I see what you are saying technically each one is incremented but they aren't always used.

Say if we had something like:


In this case, it would print out '3' because x++ is not being directly acted upon. It would simply print the value of x which would be x++ which just stores the value of '3'. But you're saying that it would be 3[4] but the 4 is never actualized in the code.


And with:


(x++) I think of it storing the current value, therefore '3' then having [4] on the side but only when it's actualized. So when (++x) needs to retrieve information from x++ then that '4' comes into play. So (++x) is '4' then gets incremented to '5' then (x++) takes the current value '5' and has [6] on the side but since nothing is directly acting upon it that [6] never gets actualized.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to let go of these concepts of "stored", "actualized" and "directly acted upon".

(x++) + (++x) + (x++) is an expression. An expression is something which, after performing all operations, has a value. An expression can consist of sub-expressions. Here's what happens when we evaluate every expression step by step:

The value of the entire expression is 13, and that's what you would get if you printed it. The side-effect of evaluating this expression is that x is now 6.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . .
y = (x++) + (++x) + (x++)
. . .
Apart from the fact that this sort of question explains why it is bad style to write such code, there is another thing. Remember that the ++ operators have a higher precedence than +, so all the () can be removed without changing the order of evaluation and execution in the slightest. Go back to the diagrams which I showed you and other people have too, only they have given you the right figures. Write down the old and new values of the variable and the whole expression. Also look at the FAQ which I mentioned earlier. We wrote that because so many people get confused by the ++ (and --) operators.

Please don't write such long // comments, which make your posts really difficult to read. Use multiline /* comments */ instead. A // comment should be sort because it usually has to fit in the end of the line after the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!