This question is just curiosity : I was wondering what would be the value of some int x
after the line x += ++x
So I tried that :
int x=10;
x+=++x;
System.out.println(x);
And it printed out :
21
After some tests with other values, it seems to be equivalent to x=2x+1. Why ? Is this line interpreted by the compiler as a byte operation ? (By the way, x += x++ seems to be equivalent to x=2x).
I don't think it's something I'd ever use in a project, but I'm curious to know why I get this result.
Thanks for any explanation or hint
EDIT : First of all, thanks for your answers
I knew how the +=
operator works, as well as the x++
and ++x
, but for some reason the (completely logic and obvious) result seemed strange to me
I should probably have thought it through, sorry for your time !