i++ 和有什么不一样? & ++i 在 for 循环中?

发布于 2024-08-22 12:24:43 字数 118 浏览 4 评论 0原文

我刚刚开始学习 Java,现在我开始使用 for 循环语句。我不明白 ++ii++ 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

活雷疯 2024-08-29 12:24:43

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

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

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

They both increment the number. ++i is equivalent to i = 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, whereas i++ increments the number after the expression is evaluated.

int i = 3;
int a = i++; // a = 3, i = 4
int b = ++a; // b = 4, a = 4
辞慾 2024-08-29 12:24:43

这是一个示例类:

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

如果我使用 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

}

如果我更改循环,使其使用 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

}

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

这说明从生成的字节码的角度来看,循环没有区别。在其他上下文中,++i 和 i++ 之间存在差异,但 for 循环没有差异。

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:

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:

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.

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.

耳钉梦 2024-08-29 12:24:43

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

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

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

如果您想知道区别,请看这个示例:

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

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

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

Both of them increase the variable i by 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:

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 ++a increments a and returns that value, while a++ returns a's value and then increments a.

很酷不放纵 2024-08-29 12:24:43

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.

失去的东西太少 2024-08-29 12:24:43

区别在于,后自增运算符 i++ 返回 i,因为它在自增之前,而前自增运算符 ++ i 返回 i,因为它是递增后的。如果您询问典型的 for 循环:

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

或者

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

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

The difference is that the post-increment operator i++ returns i as it was before incrementing, and the pre-increment operator ++i returns i as it is after incrementing. If you're asking about a typical for loop:

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 ++i as a part of a larger expression.

忆离笙 2024-08-29 12:24:43

i++++i 都是 i = i + 1 的简写形式。

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

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

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

相同,

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

未使用该部分的值,因此上面的内容与or

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

在这些情况下,区别在于( i++++i 之间)

while(i++ < 10)

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

Both i++ and ++i are short-hand for 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).

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

while(i++ < 10)

for (int i=0; i++ < 10; )
红墙和绿瓦 2024-08-29 12:24:43

JLS§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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文