i++ 和有什么不一样? & ++i 在 for 循环中?
我刚刚开始学习 Java,现在我开始使用 for 循环语句。我不明白 ++i
和 i++
在 for 循环中如何工作。
它们如何进行加法和减法等数学运算?
I've just started learning Java and now I'm into for loop statements. I don't understand how ++i
and i++
works in a for-loop.
How do they work in mathematics operations like addition and subtraction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
他们都增加了数字。
++i
相当于i = i + 1
。i++
和++i
非常相似,但并不完全相同。两者都会递增数字,但++i
在计算当前表达式之前递增数字,而i++
在计算表达式之后递增数字。They both increment the number.
++i
is equivalent toi = i + 1
.i++
and++i
are very similar but not exactly the same. Both increment the number, but++i
increments the number before the current expression is evaluted, whereasi++
increments the number after the expression is evaluated.这是一个示例类:
如果我使用 javap.exe 反汇编该类,我会得到以下结果:
如果我更改循环,使其使用 i++ 并再次反汇编,我会得到以下结果:
当我比较两者时,TextPad 告诉我两者是相同的。
这说明从生成的字节码的角度来看,循环没有区别。在其他上下文中,++i 和 i++ 之间存在差异,但 for 循环没有差异。
Here's a sample class:
If I disassemble this class using javap.exe I get this:
If I change the loop so it uses i++ and disassemble again I get this:
When I compare the two, TextPad tells me that the two are identical.
What this says is that from the point of view of the generated byte code there's no difference in a loop. In other contexts there is a difference between ++i and i++, but not for loops.
他们都将变量
i
加一。这就像说i = i + 1
。差异是微妙的。如果您在这样的循环中使用它,则没有区别:如果您想知道区别,请看这个示例:
其想法是
++a
递增a 并返回该值,而
a++
返回 a 的值,然后递增a
。Both of them increase the variable
i
by one. It's like sayingi = i + 1
. The difference is subtle. If you're using it in a loop like this, there's no difference:If you want to know the difference, look at this example:
The idea is that
++a
incrementsa
and returns that value, whilea++
returns a's value and then incrementsa
.for循环的处理方式如下:
1 首先进行初始化(i=0)
2 进行检查(i < n)
3 执行循环中的代码。
4 值递增
5 重复步骤 2 - 4
这就是为什么在使用的 for 循环中 i++ 和 ++i 没有区别的原因。
The way for loop is processed is as follows
1 First, initialization is performed (i=0)
2 the check is performed (i < n)
3 the code in the loop is executed.
4 the value is incremented
5 Repeat steps 2 - 4
This is the reason why, there is no difference between i++ and ++i in the for loop which has been used.
区别在于,后自增运算符
i++
返回i
,因为它在自增之前,而前自增运算符++ i
返回i
,因为它是递增后的。如果您询问典型的for
循环:或者
它们完全相同,因为您没有使用
i++
或++i
作为更大表达式的一部分。The difference is that the post-increment operator
i++
returnsi
as it was before incrementing, and the pre-increment operator++i
returnsi
as it is after incrementing. If you're asking about a typicalfor
loop:or
They're exactly the same, since you're not using
i++
or++i
as a part of a larger expression.i++
和++i
都是i = i + 1
的简写形式。除了更改 i 的值之外,它们还会在加一之前 (
i++
) 或加一之后 (++i
) 返回 i 的值。在循环中,第三个组件是每次迭代后执行的一段代码。
相同,
未使用该部分的值,因此上面的内容与or
在这些情况下,区别在于(
i++
和++i
之间)Both
i++
and++i
are short-hand fori = i + 1
.In addition to changing the value of i, they also return the value of i, either before adding one (
i++
) or after adding one (++i
).In a loop the third component is a piece of code that is executed after each iteration.
The value of that part is not used, so the above is just the same as
or
Where it makes a difference (between
i++
and++i
)is in these casesJLS§14.14.1, basic for Statement,明确表示对 ForUpdate 表达式进行求值,并丢弃值。其效果是使
for
语句上下文中的两种形式相同。JLS§14.14.1, The basic for Statement, makes it clear that the ForUpdate expression(s) are evaluated and the value(s) are discarded. The effect is to make the two forms identical in the context of a
for
statement.