Java:递增/递减运算符的前缀/后缀

发布于 2024-10-25 20:29:17 字数 558 浏览 1 评论 0原文

从下面的程序或此处,为什么最后一次调用 System.out.println(i) 打印值 7

class PrePostDemo {
    public static void main(String[] args) {
        int i = 3;
        i++;

        System.out.println(i);    // "4"

        ++i;
        System.out.println(i);    // "5"
        System.out.println(++i);  // "6"
        System.out.println(i++);  // "6"
        System.out.println(i);    // "7"
    }
}

From the program below or here, why does the last call to System.out.println(i) print the value 7?

class PrePostDemo {
    public static void main(String[] args) {
        int i = 3;
        i++;

        System.out.println(i);    // "4"

        ++i;
        System.out.println(i);    // "5"
        System.out.println(++i);  // "6"
        System.out.println(i++);  // "6"
        System.out.println(i);    // "7"
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

情徒 2024-11-01 20:29:17
i = 5;
System.out.println(++i); //6

这会打印出“6”,因为它接受 i,加 1,然后返回值:5+1=6。这是前缀,在操作中使用之前添加到数字中。

i = 6;
System.out.println(i++); //6 (i = 7, prints 6)

这会打印出“6”,因为它需要 i,存储一个副本,向变量加 1,然后返回该副本。这样你就得到了 i 的值,但同时也增加了它。因此,您打印出旧值,但它会增加。后缀增量的美妙之处。

然后,当您打印出 i 时,它会显示 i 的实际值,因为它已增加:7。

i = 5;
System.out.println(++i); //6

This prints out "6" because it takes i, adds one to it, and returns the value: 5+1=6. This is prefixing, adding to the number before using it in the operation.

i = 6;
System.out.println(i++); //6 (i = 7, prints 6)

This prints out "6" because it takes i, stores a copy, adds 1 to the variable, and then returns the copy. So you get the value that i was, but also increment it at the same time. Therefore you print out the old value but it gets incremented. The beauty of a postfix increment.

Then when you print out i, it shows the real value of i because it had been incremented: 7.

烟─花易冷 2024-11-01 20:29:17

另一种说明方式是:

++i 将给出new i 的结果,i++ 将给出原始的结果>i 并存储新的 i 以供下一步操作。

一种思考方式是,在表达式中做其他事情。当您打印 i 的当前值时,它将取决于 i 是在表达式内还是在表达式之后发生更改。

int i = 1;
result i = ++i * 2 // result = 4, i = 2

i 在计算结果之前进行评估(更改)。打印此表达式的 i,显示用于此表达式的 i 的更改值。

result i = i++ * 2 // result = 2, i = 2

i 在计算结果后进行评估。因此,从此表达式打印 i 给出了该表达式中使用的 i 的原始值,但仍会更改 i 以供进一步使用。因此,在表达式之后立即打印 i 的值,将显示 i 的新增量值。由于i的值发生了变化,无论是打印还是使用。

result i = i++ * 2 // result = 2, i = 2
System.out.println(i); // 2

如果您保持一致的模式并包含所有值的打印行:

int i = 3;
System.out.println(i);    //  3
System.out.println(i++);  //  3
System.out.println(i);    // "4"
System.out.println(++i);  //  5
System.out.println(i);    // "5"
System.out.println(++i);  // "6"
System.out.println(i++);  // "6"
System.out.println(i);    // "7"

Another way to illustrate it is:

++i will give the result of the new i, i++ will give the result of the original i and store the new i for the next action.

A way to think of it is, doing something else within the expression. When you are printing the current value of i, it will depend upon whether i has been changed within the expression or after the expression.

int i = 1;
result i = ++i * 2 // result = 4, i = 2

i is evaluated (changed) before the result is calculated. Printing i for this expression, shows the changed value of i used for this expression.

result i = i++ * 2 // result = 2, i = 2

i is evaluated after the result in calculated. So printing i from this expression gives the original value of i used in this expression, but i is still changed for any further uses. So printing the value for i immediately after the expression, will show the new incremented value of i. As the value of i has changed, whether it is printed or used.

result i = i++ * 2 // result = 2, i = 2
System.out.println(i); // 2

If you kept a consistent pattern and included print lines for all the values:

int i = 3;
System.out.println(i);    //  3
System.out.println(i++);  //  3
System.out.println(i);    // "4"
System.out.println(++i);  //  5
System.out.println(i);    // "5"
System.out.println(++i);  // "6"
System.out.println(i++);  // "6"
System.out.println(i);    // "7"
以可爱出名 2024-11-01 20:29:17

++ii++ 视为与 i = i+1类似 但它不一样。不同之处在于 i 获取新增量的时间。

++i 中,增量立即发生。

但如果存在i++,则当程序转到下一行时就会发生增量。

看看这里的代码。

int i = 0;
while(i < 10){
   System.out.println(i);
   i = increment(i);
}

private int increment(i){
   return i++;
}

这将导致无休止的循环。因为i将返回原始值,在分号之后,i将增加,但返回值并未增加。因此,i实际上永远不会作为递增值返回。

Think of ++i and i++ as similar to i = i+1. But it is not the same. The difference is when i gets the new increment.

In ++i , the increment happens immediately.

But if i++ is there, the increment will happen when the program goes to the next line.

Look at the code here.

int i = 0;
while(i < 10){
   System.out.println(i);
   i = increment(i);
}

private int increment(i){
   return i++;
}

This will result in a nonending loop. Because i will be returned with the original value and after the semicolon, i will get incremented, but the returned value has not been. Therefore i will never actually returned as an incremented value.

心奴独伤 2024-11-01 20:29:17

为什么变量没有被更新?

  • 后缀:将 i 的当前值传递给函数,然后将其递增。
  • 前缀:增加当前值,然后将其传递给函数。

你不与我做任何事情的行没有什么区别。

请注意,这对于作业也是如此:

i = 0;
test = ++i;  // 1
test2 = i++; // 1

Why wouldn't the variable have been updated?

  • Postfix: passes the current value of i to the function and then increments it.
  • Prefix: increments the current value and then passes it to the function.

The lines where you don't do anything with i make no difference.

Notice that this is also true for assignments:

i = 0;
test = ++i;  // 1
test2 = i++; // 1
完美的未来在梦里 2024-11-01 20:29:17
System.out.println(i++);  // "6"

这将发送 println 我在这行代码 (6) 之前获得的值,然后将 I 递增(到 7)。

System.out.println(i++);  // "6"

This sends println the value I had prior to this line of code (6), and then increments I (to 7).

无所谓啦 2024-11-01 20:29:17

实际运算符如何实现的示例:

class Integer {
  private int __i;

  function Integer ++() { // Prefix operator i.e. ++x
    __i += 1; // Increment
    return this; // Return the object with the incremented value
  }

  function Integer ++(Integer x) { // Postfix operator, i.e., x++
    __i+=1; // Increment
    return x; // Return the original object
  }
}

An example of how the actual operators are implemented:

class Integer {
  private int __i;

  function Integer ++() { // Prefix operator i.e. ++x
    __i += 1; // Increment
    return this; // Return the object with the incremented value
  }

  function Integer ++(Integer x) { // Postfix operator, i.e., x++
    __i+=1; // Increment
    return x; // Return the original object
  }
}
凡尘雨 2024-11-01 20:29:17

也许通过这个例子你可以更好地理解 Prefix/postfix。

public class TestPrefixPostFix
{
    public static void main (String[] args)
    {
        int x = 10;
        System.out.println((x++ % 2 == 0) ? "yes " + x: " no " + x);
        x = 10;
        System.out.println((++x % 2 == 0) ? "yes " + x: " no " + x);
    }
}

Maybe you can understand better Prefix/postfix with this example.

public class TestPrefixPostFix
{
    public static void main (String[] args)
    {
        int x = 10;
        System.out.println((x++ % 2 == 0) ? "yes " + x: " no " + x);
        x = 10;
        System.out.println((++x % 2 == 0) ? "yes " + x: " no " + x);
    }
}
乞讨 2024-11-01 20:29:17

这可能很容易理解:

package package02;

public class C11PostfixAndPrefix {

    public static void main(String[] args) {
        // In this program, we will use the value of x for understanding prefix 
        // and the value of y for understaning postfix. 
        // Let's see how it works. 
        
        int x = 5; 
        int y = 5; 
        
        Line 13:   System.out.println(++x);  // 6   This is prefixing. 1 is added before x is used. 
        Line 14:   System.out.println(y++);  // 5   This is postfixing. y is used first and 1 is added. 
        
        System.out.println("---------- just for differentiating");
        
        System.out.println(x);  // 6   In prefixing, the value is same as before {See line 13}
        System.out.println(y);  // 6   In postfixing, the value increases by 1  {See line 14} 
        
        // Conclusion: In prefixing (++x), the value of x gets increased first and the used 
        // in an operation. While, in postfixing (y++), the value is used first and changed by
        // adding the number. 
    }
}

This may be easy to understand:

package package02;

public class C11PostfixAndPrefix {

    public static void main(String[] args) {
        // In this program, we will use the value of x for understanding prefix 
        // and the value of y for understaning postfix. 
        // Let's see how it works. 
        
        int x = 5; 
        int y = 5; 
        
        Line 13:   System.out.println(++x);  // 6   This is prefixing. 1 is added before x is used. 
        Line 14:   System.out.println(y++);  // 5   This is postfixing. y is used first and 1 is added. 
        
        System.out.println("---------- just for differentiating");
        
        System.out.println(x);  // 6   In prefixing, the value is same as before {See line 13}
        System.out.println(y);  // 6   In postfixing, the value increases by 1  {See line 14} 
        
        // Conclusion: In prefixing (++x), the value of x gets increased first and the used 
        // in an operation. While, in postfixing (y++), the value is used first and changed by
        // adding the number. 
    }
}
热风软妹 2024-11-01 20:29:17

它为最后一个语句打印7,因为在上面的语句中,它的值为6,并且当打印最后一个语句时它会增加到7。

It prints 7 for the last statement, because in the statement above, its value is 6 and it's incremented to 7 when the last statement gets printed.

清引 2024-11-01 20:29:17

这可能会有所帮助......我也需要理解这个难题。

public class Main {
  public static void main(String[] args) {

    int x = 5;
    int y = 5;

    System.out.println(++x);
    System.out.println(y++);

    System.out.println("------");

    System.out.println(x);
    System.out.println(y);
  }
}

This might help... it also took my to understand this puzzle.

public class Main {
  public static void main(String[] args) {

    int x = 5;
    int y = 5;

    System.out.println(++x);
    System.out.println(y++);

    System.out.println("------");

    System.out.println(x);
    System.out.println(y);
  }
}
女皇必胜 2024-11-01 20:29:17

好吧,从临时变量的角度来考虑它。

i = 3;
i ++; // Is equivalent to:   temp = i++; and so, temp = 3 and then "i" will increment and become i = 4;
System.out.println(i); // Will print 4

现在,

i = 3;
System.out.println(i++);

相当于

temp = i++;  // 'temp' will assume a value of the current "i", after which "i" will increment and become i = 4
System.out.println(temp); // We're printing 'temp' and not "i"

Well, think of it in terms of temporary variables.

i = 3;
i ++; // Is equivalent to:   temp = i++; and so, temp = 3 and then "i" will increment and become i = 4;
System.out.println(i); // Will print 4

Now,

i = 3;
System.out.println(i++);

is equivalent to

temp = i++;  // 'temp' will assume a value of the current "i", after which "i" will increment and become i = 4
System.out.println(temp); // We're printing 'temp' and not "i"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文