++Var 和 Var++ 之间的区别
在编程中,特别是在 Java 中,以下之间有什么区别:
int var = 0;
var++;
和
int var = 0;
++var;
这会对 for 循环产生什么影响?
例如
for (int i = 0; i < 10; i++) {}
for (int i = 0; i < 10; ++i) {}
In programming, particularly in Java, what is the difference between:
int var = 0;
var++;
and
int var = 0;
++var;
What repercussions would this have on a for loop?
e.g.
for (int i = 0; i < 10; i++) {}
for (int i = 0; i < 10; ++i) {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
太棒了;
尽管
var++
和++var
都会递增它们所应用的变量,但var++
返回的结果是变量递增之前的值,而++var
返回的结果是应用增量后变量的值。进一步说明
当
++var
或var++
形成完整的语句(如您的示例中)时,两者之间没有区别。例如,以下内容与然而
,当
++var
或var++
用作较大语句的一部分时,两者可能不等效。例如,以下断言通过,而此断言失败
虽然
var++
和++var
都递增了它们所应用的变量,但返回的结果var++
是变量递增之前的值,而++var
返回的结果是应用递增之后变量的值。当在 < code>for 循环,两者没有区别,因为增量变量的值不构成较大语句的一部分。可能不会这样出现,因为源文件的同一行上还有其他代码。但如果仔细观察,您会发现增量之前有一个
;
,后面没有任何内容,因此增量运算符不构成较大语句的一部分。tldr;
Although both
var++
and++var
increment the variable they are applied to, the result returned byvar++
is the value of the variable before incrementing, whereas the result returned by++var
is the value of the variable after the increment is applied.Further Explanation
When
++var
orvar++
form a complete statement (as in your examples) there is no difference between the two. For example the followingis identical to
However, when
++var
orvar++
are used as part of a larger statement, the two may not be equivalent. For example, the following assertion passeswhereas this one fails
Although both
var++
and++var
increment the variable they are applied to, the result returned byvar++
is the value of the variable before incrementing, whereas the result returned by++var
is the value of the variable after the increment is applied.When used in a
for
loop, there is no difference between the two because the incrementation of the variable does not form part of a larger statement. It may not appear this way, because there is other code on the same line of the source file. But if you look closely, you'll see there is a;
immediately before the increment and nothing afterwards, so the increment operator does not form part of a larger statement.int a = 5, b;
后增量:
b = a++;
:a
首先传输到b
,然后a
递增,所以现在b
是5
,a
是 < code>6 效果是b = a; a = a + 1;
预自增:
b = ++a;
:首先将a
自增,然后结果为转移到b
,所以现在a
是7
,b
也是7
。效果是a = a + 1; b = a
a++
和++a
保持独立,以类似的方式起作用。在您提供的循环示例中,增量运算符在任何表达式中都不关联,并且是独立的。因此,在这个特定的实现中,这两者是相同的。int a = 5, b;
post increment :
b = a++;
:a
is first transferred tob
and thena
is incremented, so nowb
is5
, anda
is6
The effect isb = a; a = a + 1;
pre increment:
b = ++a;
: firsta
is incremented and then the result is transferred intob
, so nowa
is7
and alsob
is7
. The effect isa = a + 1; b = a
a++
and++a
staying independently act in the similar way. In the loop examples you have presented, the increment operators is not associated in any expression, and are independent. Therefore these two in this particular implementation is identical.++var
是预自增运算符;它在计算表达式之前增加var
的值。同样,var++
是后置自增运算符;在计算表达式之后,它会增加var
的值。在简单循环的情况下,两者之间没有区别,因为表达式
++var;
和var++;
都会产生相同的结果。有关详细信息,请参阅例如 http://www. article.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#IncDecOps。
++var
is the pre-increment operator; it increments the value ofvar
before evaluating the expression. Similarly,var++
is the post-increment operator; it increments the value ofvar
after evaluating the expression.In the case of a simple loop, there is no difference between two, because the expressions
++var;
andvar++;
both yield to the same result.For more info, see for example http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter02/operators.html#IncDecOps.
var++ 在递增之前返回其值。
++var 在递增后返回其值。
var++ returns its value before incrementing.
++var returns its value after incrementing.
在您的示例中,没有区别,但是以下之间有区别:
和:
在第一种情况下,var2 的值为 1,而在第二种情况下,它为 0。
In your examples, there is no difference, there is however a difference between:
and:
In the first case, the value of var2 is 1 while in the second, it's 0.
当单独出现在表达式中时,
++var
和var++
是相同的。这适用于您的问题,因为您只有
++i
,i++
当您内联它们时才会发生差异:
如何记住?
只有 您会看到首先是运算符,然后是增量,最后是取值。
当您首先看到变量,然后获取值并随后递增时。
因此,在第一个示例中,您会看到相同的值,因为:
因此,在第二个示例中,您会看到差异,因为:
Both
++var
andvar++
are identical when appear in expression alone.This applies to your question because you have alone
++i
,i++
It difference only take place when you inline them:
How to remember?
When you see first the operator, then increment and later take value.
When you see first the variable, then take value and later increment.
So in first example you see equal values because:
So in second example you see differences because:
++i 是预自增,它在出现的表达式中的任何内容之前完成。
i++ 是后递减,它是在它出现的表达式中的任何内容之后完成的。
在第一个循环中,您将从 0 运行到 9。在第二个循环中,它将从 1 运行到 9。
我的建议:避免这两个循环(例外可能是 i++;或第一个循环)。在表达式中寻找太棘手了。
我花了一整天的时间调试
myArray[(i++)%16] = data
并试图找出为什么它有时会尝试写入 data[16]
++i is preincrement, it is done before anything in the expression it appears.
i++ is postdecrement, it is done after anything in the expression it appears.
in the first loop, you'll run from 0 to 9. In the second, it will run from 1 to 9.
My advice: avoid both of them (exceptions may be i++; or the first loop). Too tricky to look for in the expression.
I passed a long day debugging
myArray[(i++)%16] = data
and trying to find why it did try to write to data[16] sometimes
事实上,这很简单。语句中首先执行预增量 ++i。
相当于
最后执行后置增量 i++;
相当于
In fact, this is rather simple. The preincrement ++i is executed first in the statement.
is equivalent to
whereas the post increment i++ is executed at the end;
is equivalent to