r/javahelp • u/ShitStormLord • Jun 15 '20
Need help understanding this.
Hi! I'm reading java for dummies 7. I came across something that doesn't makes sense to me, would you help me out?
This is my code:
int i = 20;
i = i++ + i++;
System.out.println(i);
The output is 41. But it doesn't make sense in my head. I know that if the ++ operator is at the end, then it keeps the original value until the end of the sentence, and after the expression is evaluated, it adds 1. So for me this would be like:
i = 20 + 20;
And once the sentence is over, it turn to 42, for I'm using ++ two times. I understand its something like i=20(+1) + 20(+1).
So why the result is 41? I can't understand it. Hopefully I've explained my self! Thanks for the help!
EDIT: Thank you very much for all your answers! I understand now!
6
u/dynamo_girl02 Jun 15 '20
see lets take an example
ex1 int i = 5;
int j = ++i; // pre increment
++ before a variable in this case first increments the default value of i = 5 to 6 and thus
stores the value to j and finally j=6 , i=6
ex1 int i= 5;
i = i++; // post increment
++ after a variable in this case first stores the value of i = 5 to j and than increments itself from i=5 6 so finally the values of j=5,i=6
in your problem
i = i++ +i++;
so firstly for first i++
value used for i would be 20 but then incremented to 21
so i = 20 + i++ ; // i=21
then similarly for second i++value used for i would be 21 but then incremented to 22
i = 20 + 21 ;// i=22
i=41
5
u/GioVoi Jun 15 '20
To perhaps extend on this example, try to see what happens with ++i instead. So if we had the code
int i = 20;
i = ++i + ++i;
System.out.println(i);What would this output? Why is it different? This might help to understand the differences & what is happening each step of the way
3
3
Jun 15 '20
I like the explanation already provided on this question, because it's absolutely true. But, for a Java newbie this may not be clear, so to help illustrate let's walk through it like a story. It's good that you know how the increment operator works, where it executes after the expression is evaluated, but knowing where exactly that evaluation happens can be kind of confusing. I hope you don't mind, I'd like to ELI5 this process. Note that I'm not a Java expert, so if anyone has corrections to make please feel free.
So, picture you standing next to a dumb robot named Java, who's writing stuff up on a whiteboard. This whiteboard is, to put it simply, Java's stack memory while it's executing a method. Here's how the process goes.
You: "Hey, Java, I'm going to give you a variable. It's an int. When I want to refer to that variable, I'm going to use the letter i."
Java: "Okay. Cool. I need to remember that there's a variable named i and it points to a primitive int value." It writes int i on the board.
You: "That variable's value is 20."
Java: "Okay. The variable i has a value of 20." and it writes = 20 next to the i. This value is now stored in memory; a reference to the number 20 exists for i, and whenever you say i you actually mean 20.
You: "Alright Java, we're going to change the value of the variable i."
Java: "Okay, cool. I know about the variable i, I've been told it means 20. I'm ready to change that. What are we changing it to?"
You: "We're going to figure it out with a math problem. But first, tell me the value of i because we're going to be using it." (this is the evaluation phase of the expression before the increment operator executes - you're asking Java to evaluate what i is right now.)
Java: "Oh, I know what i is! I wrote it up above (made a reference in stack memory). The value of i is 20." *Java writes the number 20 down on a new line in preparation to do some math with it. It DOESN'T write down i, it only writes down 20.*
You: "Okay, once you're done figuring out what i is, I want you to add 1 to whatever i is." (++)
Java: "Okay! It looks like I finished doing an evaluation on i. That statement is complete. You told me that once I figured out what i was, you wanted me to add 1 to it. Looks like the value is 20 right now, so I'm going to go ahead and make i equal to 21 right now." It erases the = 20 next to the i at the top of the whiteboard, and puts = 21 in its place.
It's important to remember at this point that Java already wrote down 20 below in preparation for some math; that number doesn't change. i is a variable, and was at one point a reference to the number 20, but 20 is a primitive value, and it doesn't mean that it's equal to i.
At this point, you repeat the last 4 steps again - you ask Java what the value of i is, which is now 21. It writes 21 down below the 20 from before, and then when it's done figuring out what the value of i is, it erases the = 21 next to i and puts = 22 in its place.
You: "Okay, you giant overblown calculator. You've done everything I asked you to do in preparation to figure out the final value of the variable i. Now, do the math you wrote down below and figure out what I want i to be.
Java will perform the math you told it to on the numbers it wrote down (20 and 21), which comes out to be 41. Java will erase the = 22 next to i and write in = 41 because that's that 20 + 21 is. Ergo, i prints out 41.
Note that you could've had it evaluate int x = i++ + i++ and it would've given you the same answer when you spat out x, 41. But at that point, i would've been 22.
0
28
u/trisul-108 Jun 15 '20
Go through it step by step. It increments after using the value, not at the end of the statement.
So, the first time, it uses 20 and increments i to 21. The second time it uses 21 and increments i to 22. Next it adds 20 and 21 to get 41 overwriting the 22.