New to JAVA, and I know there have been some discussions about ++i and i++, but I still don't know how to approach the question below and how to explain the answer. I shared what I think, but I hope you can help correct what's wrong with my thinking. Thank you very much!
The question is:
Give the results of the following java program.
int z = -1;
system.out.println(++z);
(My thinking: pre-increment, which is incrementing the value of i by 1, so the output is -1+1 = 0)
system.out.println(z--);
(My thinking: pre-decrement, which is decrementing the value of i by 1, so the output is -1-1 = -2, but why is 0? )
system.out.println(z++ + z);
(My thinking: post-increment, which is incrementing the value of i by 1.So the entire thing reads as (z++ plus z). So z++ is 0, + (plus) z, which is 0 + (-1), so the output is -1)
system.out.println(z + z++);
(My thinking: the entire thing reads as (z plus z++). So z is -1, plus z++, which is 0, so the output is -1, but the answer is 0.)
The answer is: 0 0 -1 0
What the answer would be if we changed every + to -?
int z = -1;
system.out.println(--z);
system.out.println(z-- - z);
system.out.println(z - z--);
After reading lots of discussions, I still feel confused. Really hope to learn to understand the concepts. Thank you!