Java for 循环中的 i++ 和 ++i 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2315705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:02:31  来源:igfitidea点击:

What is the difference between i++ & ++i in a for loop?

javaincrement

提问by user228330

I've just started learning Java and now I'm into for loop statements. I don't understand how ++iand i++works in a for-loop.

我刚刚开始学习 Java,现在我开始学习 for 循环语句。我不知道如何++ii++工作在一个for循环。

How do they work in mathematics operations like addition and subtraction?

它们在加法和减法等数学运算中是如何工作的?

回答by David Johnstone

They both increment the number. ++iis equivalent to i = i + 1.

他们都增加了数字。++i相当于i = i + 1

i++and ++iare very similar but not exactly the same. Both increment the number, but ++iincrements the number before the current expression is evaluted, whereas i++increments the number after the expression is evaluated.

i++并且++i非常相似但不完全相同。两者都增加数字,但++i在计算当前表达式之前增加数字,而i++在计算表达式之后增加数字。

int i = 3;
int a = i++; // a = 3, i = 4
int b = ++a; // b = 4, a = 4

回答by Thilo

Both i++and ++iare short-hand for i = i + 1.

这两个i++++i是短手i = 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).

除了改变 i 的值,它们还返回 i 的值,在加一 ( i++)之前或加一 ( ++i) 之后。

In a loop the third component is a piece of code that is executed after each iteration.

在循环中,第三个组件是在每次迭代后执行的一段代码。

for (int i=0; i<10; i++)

The value of that part is not used, so the above is just the same as

那部分的值没有使用,所以上面的就和

for(int i=0; i<10; i = i+1)

or

或者

for(int i=0; i<10; ++i)

Where it makes a difference (between i++and ++i)is in these cases

在这些情况下(i++++i)产生区别的地方

while(i++ < 10)

for (int i=0; i++ < 10; )

回答by Carl Norum

The difference is that the post-increment operator i++returns ias it was beforeincrementing, and the pre-increment operator ++ireturns ias it is afterincrementing. If you're asking about a typical forloop:

不同的是,后增量运算i++回报i,因为它是以前增加,和预增操作符++i的回报i,因为它是递增。如果您要问一个典型的for循环:

for (i = 0; i < 10; i++)

or

或者

for (i = 0; i < 10; ++i)

They're exactly the same, since you're not using i++or ++ias a part of a larger expression.

它们完全相同,因为您没有使用i++++i作为更大表达式的一部分。

回答by Javier

Both of them increase the variable iby one. It's like saying i = i + 1. The difference is subtle. If you're using it in a loop like this, there's no difference:

它们都将变量i加一。就像在说i = i + 1。区别很微妙。如果您在这样的循环中使用它,则没有区别:

for (int i = 0; i < 100; i++) {
}

for (int i = 0; i < 100; ++i) {
}

If you want to know the difference, look at this example:

如果你想知道区别,看看这个例子:

int a = 0;
int b = a++; // b = 0; a = 1

a = 0;
b = ++a: // b = 1; a = 1

The idea is that ++aincrements aand returns that value, while a++returns a's value and then increments a.

这个想法是++a递增a并返回该值,而a++返回 a 的值然后递增a

回答by duffymo

Here's a sample class:

这是一个示例类:

public class Increment
{
    public static void main(String [] args)
    {
        for (int i = 0; i < args.length; ++i)
        {
            System.out.println(args[i]);
        }
    }
}

If I disassemble this class using javap.exe I get this:

如果我使用 javap.exe 反汇编这个类,我会得到这个:

Compiled from "Increment.java"
public class Increment extends java.lang.Object{
public Increment();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge       23
   8:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

}

If I change the loop so it uses i++ and disassemble again I get this:

如果我更改循环以便它使用 i++ 并再次反汇编,我会得到这个:

Compiled from "Increment.java"
public class Increment extends java.lang.Object{
public Increment();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge       23
   8:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

}

When I compare the two, TextPad tells me that the two are identical.

当我比较两者时,TextPad 告诉我两者是相同的。

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++ 之间存在差异,但 for 循环不存在差异。

回答by java_geek

The way for loop is processed is as follows

for循环的处理方式如下

1 First, initialization is performed (i=0)

1 首先进行初始化(i=0)

2 the check is performed (i < n)

2 执行检查 (i < n)

3 the code in the loop is executed.

3 执行循环中的代码。

4 the value is incremented

4 值递增

5 Repeat steps 2 - 4

5 重复步骤 2 - 4

This is the reason why, there is no difference between i++ and ++i in the for loop which has been used.

这就是为什么在使用的 for 循环中 i++ 和 ++i 之间没有区别的原因。

回答by trashgod

JLS§14.14.1, The basic for Statement, makes it clear that the ForUpdateexpression(s) are evaluated and the value(s) are discarded. The effect is to make the two forms identical in the context of a forstatement.

JLS§14.14.1,Statement 的基础,清楚地表明ForUpdate表达式被评估并且值被丢弃。其效果是在for语句的上下文中使两种形式相同。