java weird increment syntax
So I have always been taught that in Java, using the increment operator
after a variable name in an expression will do the expression, and then
increment the value and using the operator before a variable name in an
expression will do the increment before the evaluation. Like this:
int x = 0;
int y = x++;
after this executes y should be 0 and x should be 1. and in this example
int x = 0;
int y = ++x;
should be x = 1 and y = 1.
Following that same logic, the following...
int x = 0;
int y = 0;
x = y++ - y++;
should output 0 as x and 2 as y because 0 - 0 = 0. However the output is
x = -1
y = 2
Why is this?
Edit: the value of y does not matter. x will always equal -1 and y will
(in the end) equal y + 2.
No comments:
Post a Comment