[英]x += ++x equivalent to x = 2x+1 : Why?
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 : 这个问题只是好奇心:我想知道
x += ++x
之后某些int x
的值是什么?所以我试过了:
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. 在使用其他值进行一些测试后,它似乎等于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).
(顺便说一下,x + = x ++似乎等于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 ! 我知道
+=
运算符是如何工作的,以及x++
和++x
,但由于某种原因,(完全逻辑和明显的)结果对我来说似乎很奇怪我应该已经考虑过了,抱歉你的时间!
The way it is calculated is 它的计算方法是
Step 2: It become x = 10 + (incremented x) 11 步骤2:变为x = 10 +(递增x)11
Step 3: Final result stored in x ie 21 第3步:最终结果存储在x即21中
Here is the proof: 这是证明:
I created a MainClass as below: 我创建了一个MainClass如下:
public class MainClass{
public static void main(String...s){
int x = 10;
x += ++x;
}
}
and then checked the bytecode using javap -c MainClass
然后使用
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. 它关于运算符优先级以及如何评估和使用
++x
和x++
。 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
与
++x
,值x
被增量,然后用如此++x
变为11
和这个x += ++x
变为21
,其是10 + 11
However x++
says x
is used and then its value is incremented 但是,
x++
表示使用x
,然后其值递增
so x+= x++
will mean 10 + 10
ie 20
所以
x+= x++
意味着10 + 10
即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
x + = ++x
在编译器中计算为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; 因此x = 21;
You need to understand about pre-increment(++x) and post-increment (x++). 您需要了解预增量(++ x)和后增量(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. 在上面的if条件中它将执行为true,因为首先它将比较10 == 10然后x将增加1并且x将变为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. 在上面,如果条件x将被预先递增,那么x将变为11,然后将进行比较,无论11 == 10,因此条件将失败。
Hope this will help. 希望这会有所帮助。
++x
will return value of (x+1)
, and x
value will be increased by one too. ++x
将返回(x+1)
值, x
值也将增加1。
x++
will return value of (x)
, and x
value will be increased by one too. x++
将返回(x)
值, x
值也将增加1。
so 所以
x+=++x
is the same x=x+(x+1)
, which is equivalent to x=2*x+1
x+=++x
与x=x+(x+1)
,相当于x=2*x+1
x+=++x
is the same as x=x+(x)
, which is equivalent to x=2*x
x+=++x
与x=x+(x)
,相当于x=2*x
..... ++ x:首先计算x = x + 1,然后使用x进行比较或计算实际任务..... x ++:首先比较/计算实际任务,然后计算x = x + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.