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 !
The way it is calculated is
Step 2: It become x = 10 + (incremented x) 11
Step 3: Final result stored in x ie 21
Here is the proof:
I created a MainClass as below:
public class MainClass{
public static void main(String...s){
int x = 10;
x += ++x;
}
}
and then checked the bytecode using javap -c MainClass
public static void main(java.lang.String...);
Code:
0: bipush 10 // push 10 onto stack
2: istore_1 // store 10 in local variable 1
3: iload_1 // load local variable 1 (now 10) back to stack
4: iinc 1, 1 //increment local variable 1 by 1
7: iload_1 // load local variable 1 (now 11) back to stack
8: iadd // add top 2 variable on stack ( 10 and 11)
9: istore_1 // store 21 to local variable 1
10: return
}
Its about operator precedence and how ++x
and x++
are evaluated and used. with ++x
, the value of x
is incremented and then used so ++x
becomes 11
and this x += ++x
becomes 21
which is 10 + 11
However x++
says x
is used and then its value is incremented
so x+= x++
will mean 10 + 10
ie 20
int x=10;
x+=++x;
System.out.println(x);
x + = ++x
is evaluated in the compiler to x = x + ++x
=> x = 10 + ++x
=> x = 10 + 11
=> x = 21
See here:-
x+=++x; this expression will be executed like x=x+(x+1) so x = 10 + 11
hence x = 21;
You need to understand about pre-increment(++x) and post-increment (x++). see below
int x = 10;
if (x++ == 10 )
System.out.println( "X is equal to 10");// this statement will print
in the above if condition it will execute as true because first it will compare as is 10 == 10 and then x will be incremented by one and x will become 11.
Now see below:-
if (++x == 10 )
System.out.println( "X is equal to 10");// this will not print if condition will tern false
In the above if condition x will be pre-incremented so x will become 11 and then a comparison will be done whether 11 == 10 hence if condition will failed.
Hope this will help.
++x
will return value of (x+1)
, and x
value will be increased by one too.
x++
will return value of (x)
, and x
value will be increased by one too.
so
x+=++x
is the same x=x+(x+1)
, which is equivalent to x=2*x+1
x+=++x
is the same as x=x+(x)
, which is equivalent to x=2*x
..... ++ x:首先计算x = x + 1,然后使用x进行比较或计算实际任务..... x ++:首先比较/计算实际任务,然后计算x = x + 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.