[英]How does the following statement work?
int x = 10;
x += x++;
System.out.println(x);
why the answer of above statement is 20 ? 为什么以上陈述的答案是20?
The operator += is an addition assignment operator. 运算符+ =是加法赋值运算符。 Like Alya said above,
x += x++
is equivalent to x = x + x++
, which in your case is x = 10 + 10
. 就像上面的Alya所说,
x += x++
等效于x = x + x++
,在您的情况下为x = 10 + 10
。 However, it's a very messy statement and I'll explain why towards the end of this post. 但是,这是一个非常混乱的声明,在这篇文章的结尾,我将解释原因。
Now, you're probably thinking "Why is it 20 and not 21 (10 + 11) since you have the ++?" 现在,您可能会想:“既然拥有++,为什么它是20,而不是21(10 + 11)?” and that's valid.
那是有效的。 There's actually a difference between a post-increment and a pre-increment.
后增量和前增量之间实际上是有区别的。 x++ is the post-increment and will actually evaluate the value of x first and THEN increment x, while ++x is the pre-increment which will increment x and THEN evaluate the value of x.
x ++是后递增的,实际上会先评估x的值,然后将x的值递增,而++ x是预递增的,它将递增x的值,然后评估x的值。
For example, x = 10; System.out.println(x++); System.out.println(x);
例如,
x = 10; System.out.println(x++); System.out.println(x);
x = 10; System.out.println(x++); System.out.println(x);
will print 10 and then print 11 because the first print line prints x
and THEN performs the ++ calculation, making x
11 which the next line prints. 会先打印10,然后再打印11,因为第一行打印
x
,然后执行++计算,使x
11在下一行打印。 Conversely, x = 10; System.out.println(++x); System.out.println(x);
相反,
x = 10; System.out.println(++x); System.out.println(x);
x = 10; System.out.println(++x); System.out.println(x);
will print 11 on both print statements. 将在两个打印语句上打印11。
Going back to why I said x += x++;
回到为什么我说
x += x++;
is very messy is because technically the ++ operator isn't performed in this case. 非常混乱是因为在这种情况下,技术上不会执行++运算符。
x++
is technically the same as x=x+1
and remembering that x+=y is the same as x = x+y) , the line x += x++;
x++
在技术上与x=x+1
相同,并且记住x + = y与x = x + y相同),行x += x++;
is kind of like saying x = x + (x = x + 1);
有点像说
x = x + (x = x + 1);
which is kind of weird looking because you do 2 assignment statements in one and won't actually "work how you want it". 这看起来有点怪异,因为您一次执行了2个赋值语句,而实际上不会“按您的要求工作”。 Back to your example
int x = 10; x += x++;
回到您的示例
int x = 10; x += x++;
int x = 10; x += x++;
if you print x, you will get 20 even though you could look at it as: x is now the value of x + the value of x, then finally + 1 to it. 如果您打印x,即使您看到它也将得到20:x现在是x的值+ x的值,最后是+ 1。 But unfortunately, that's not how it works.
但是不幸的是,这不是它的工作原理。
To solve your problem, if you change your code from a post-increment to a pre-increment, then it should work, ie: x+=++x;
为了解决您的问题,如果将代码从后增量更改为前增量,则它应该起作用,即:
x+=++x;
will print your 11 but I would argue the that's quite unreadable and a bit confusing. 将打印您的11,但我认为这是相当难以理解的,并且有些混乱。
x+=x; x++; System.out.println(x);
is easier to follow. 更容易理解。
x++
will execute first. x++
将首先执行。 It returns x
and then increments x
by 1
. 它返回
x
,然后递增x
的1
。
Finally, the +=
operator will add to x
the return value of x++
, which was 10
. 最后,
+=
运算符会将x++
的返回值添加到x
该返回值为10
。
Thus, x
will be 20
and it will overwrite the changes to x
by the statement x++
. 因此,
x
将为20
,它将被语句x++
覆盖对x
的更改。
So first x
is initialized to be 10
. 因此,第一个
x
初始化为10
。 Then the x++
has higher precedence so that gets carried out first. 然后,
x++
具有更高的优先级,因此必须首先执行。 the " ++
" is a post-increment in this case (because it is after the variable as opposed to pre-increment which would be ++x
). 在这种情况下,“
++
”是后递增的(因为它在变量之后,而不是预递增的++x
)。 Post-increment means "first use the variable then increment it by one" so in this case it first uses x
to be 10
then increments it to 11 after it is used. 后递增表示“首先使用变量,然后将其递增1”,因此在这种情况下,它首先将
x
用作10
然后在使用后将其递增到11。 Then we look at the " +=
" which is short hand for " x = x+x++
". 然后我们看一下“
+=
”,它是“ x = x+x++
”的简写。 so we have x = 10+10
which = 20
. 所以我们有
x = 10+10
其中= 20
If you were to carry this out again it would equal x = 20+20 = 40
. 如果要再次执行此操作,则等于
x = 20+20 = 40
。
In this particular case, the x++
isn't necessary as x
is reassigned the value after it is incremented each time. 在这种特殊情况下,不需要
x++
因为x
每次递增后都会重新分配x
值。
int x = 10; int x = 10; x += x++;
x + = x ++;
will equal to x=x+x 等于x = x + x
where x++
mean use the x
value then increament it , so it's value will be 10 其中
x++
表示使用x
值,然后对其进行消隐处理,因此它的值为10
so the result will equal 20
所以结果等于
20
if you want to see the change of the x
, see this example: 如果要查看
x
的变化,请参见以下示例:
int x = 10;
int y = 10;
y +=x++;
System.out.println(y);
System.out.println(x);
will print : 将打印:
y=20
x=11////////////according to x++ and without to overwrite it
//
// Shows how increments work.
//
int i = 0;
System.out.println(i);
i++; // Add one
System.out.println(i);
i += 2; // Add two
System.out.println(i);
i += 3; // Add three
System.out.println(i);
++i; // Add one
System.out.println(i);
i += i; // Added itself
System.out.println(i);
//
// Uses increments and assigns.
//
int v = 0;
v = i++; // Increment after value copy
System.out.println(v);
System.out.println(i);
v = ++i; // Increment before value copy
System.out.println(v);
System.out.println(i);
//Output //输出
0 - 1 3 6 7 14 14 15 16 16 0-1 3 6 7 14 14 15 16 16
x+=x++ first assigns the value to x and then increments (post-increment)
x+=++x first increments then assign the value to x (pre increment)
there are two types of increments/decrements in programming 编程中有两种增量/减量
1. pre-increment/decrement
2. post-increment/decrement
In programming both of these have same operations but differ in there nature as they both used for increment or decrement; 在编程中,这两种操作具有相同的功能,但它们的本质不同,因为它们都用于递增或递减。 they can be written as,
他们可以写成
x+=1; (increment by 1)
x-=1; (decrement by 1)
you can use a variable instead in the above cases as well 您也可以在上述情况下使用变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.