r/javahelp • u/adviceformee • Sep 12 '21
Unsolved Don't understand the difference between ++x and x++
So from my textbook I learned that these are used to increment variables.
However my textbook gives an example of an expression with them:
int x =10, y =20, z = ++x * y--;
It says "The third assignment increments x to 11 and decrements y to 19, but in computing z, it
uses the new value of x * the old value of y
How is it using the old value of y if it ALREADY decremented it to 19?
Aside from that,
if y-- evaluates to the old value of y, why does it return y-1 when used in a line by itself? Is returning a value different than evaluating the value?
37
Sep 12 '21 edited Aug 11 '25
elderly absorbed fine selective paltry encouraging books violet terrific innocent
This post was mass deleted and anonymized with Redact
7
u/adviceformee Sep 12 '21
Wow that actually helps a ton!
So if my program is like
int y=2;
y--;
System.out.println(y);
1 is printed instead of the old value of y because there is no middle step like in your examples? Making them effectively the same if they're not part of expressions?8
u/JaedenStormes Sep 12 '21
So, if the y--; is on its own line, it is completely irrelevant whether you use --y or y--. It's only if you do other stuff in the SAME line that the order of the operations factors in there.
Personally, I just prefer to do it on its own line so I don't have to scratch my head about order of operations.
2
Sep 12 '21 edited Aug 11 '25
air smart steep normal badge placid deserve license tub summer
This post was mass deleted and anonymized with Redact
1
15
u/Nightcorex_ Sep 12 '21
Although it's already answered here's a very short explanation:
++x increments x and then returns its value.
x++ returns the value of x and then increments it.
3
u/Orami9b Sep 12 '21
It did not already increment y in the expression. ++x is a pre-increment - in this case it does increment the value of x before that value is used in the expression. However, notice that y-- is a different syntax. It's the post-increment, meaning it uses the value of y as it is, and after the expression in evaluated the value of y is decremented. Why this may be confusing is usually you use either of these statements on their own lines x++; or ++x;, in which case all you notice is that the value was incremented, but they mean something different when used as part of a larger expression.
1
u/RaisinAlert Sep 12 '21
How is it using the old value of y if it ALREADY decremented it to 19?
Typically, the old value is copied to some location (you can think of that location like a separate hidden variable, or you can think of it as being treated like a return value as if the pre-increment/decrement was a function) before the variable itself is actually incremented or decremented. If, when evaluating an enclosing expression, the computer needs the old value, it just looks in that memory location.
1
u/bansalmunish Sep 12 '21
y--
mean (get first and then decrement)
--y
means (decrement first and then get)
1
u/hugthemachines Sep 12 '21
I have seen this many times during the years in forum discussions and quiestions. I do wonder, however, how many sensible programmers that use ++x at the time they use the variable. Most code I see, the incrementation is not together with using it so it does not matter what you use anyway-
1
u/SaylorMan1496 Sep 15 '21
It does it in the order that they are tokenized by the compiler, I had an old prof always say
“Prefer prefix” because of this
y- - uses the old value of y in the above arithmetic and then decrements the value So it would result as z = 219 because (1+10) * 20 - 1
It is how the compiler treats the shorthands, but like I said to avoid confusion “prefer prefix”
-2
u/Neat-Guava5617 Sep 12 '21
Don't.
Anybody who needs others to discern between the two is a bad programmer.
It's a 'neat' trick to skip out on typing an easier to understand code. And with neat I mean somebody flexing their obscure knowledge and enforcing others to know it, thus wasting time of others. In other words, if you flex to make your peers spend effort on understanding your code, you suck as a teammate since you're not caring about them. And a programmer who doesn't care about others doesn't care about himself either, because chances are in 2 months time he'll be scratching his head what this unintelligible piece of code does...
•
u/AutoModerator Sep 12 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.