Difference between b = b ++ and b ++
4 answers
In Java, the expression
b = b++
equivalent to
int tmp = b; b = b + 1; b = tmp;
Hence the result.
(In some other languages, the same expression has unspecified behavior. See Undefined behavior and sequence points .)
+6
source to share
b++
matches:
int temp = b;
b = b + 1;
return temp;
As you can see, b++
will return its previous value, but overwrite the value b
. But since you assigned the return (old) value to b
, the new value will be overwritten and therefore "ignored".
The difference would be if you write:
b = ++b; //exactly the same as just "++b"
In this case, the increment is performed and the new value will be returned.
+1
source to share