Emi*_*l H 279

++ x称为preincrement,而x ++称为postincrement.

int x = 5, y = 5;

System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6

System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6
Run Code Online (Sandbox Code Playgroud)

  • 好的解释,1 ++.哎呀,++ 1 :) (53认同)

Vic*_*tor 63

++ x递增x的值然后返回x
x ++返回x的值然后递增

例:

x=0;
a=++x;
b=x++;
Run Code Online (Sandbox Code Playgroud)

代码运行后,a和b都将为1,但x将为2.

  • +1很多例子,这是一个带有例子的_explanation_ :) (9认同)

Pab*_*jim 17

这些被称为后缀和前缀运算符.两者都会在变量中加1,但语句的结果会有差异.

int x = 0;
int y = 0;
y = ++x;            // result: y=1, x=1

int x = 0;
int y = 0;
y = x++;            // result: y=0, x=1
Run Code Online (Sandbox Code Playgroud)


Joh*_*iss 10

是,

int x=5;
System.out.println(++x);
Run Code Online (Sandbox Code Playgroud)

将打印6

int x=5;
System.out.println(x++);
Run Code Online (Sandbox Code Playgroud)

将打印5.


Alb*_*rto 8

我在这里登陆,从最近的一个DUP的,虽然这个问题比回答更,我忍不住反编译的代码,并加入'另一个回答’:-)

为了准确(可能有点迂腐),

int y = 2;
y = y++;
Run Code Online (Sandbox Code Playgroud)

编译成:

int y = 2;
int tmp = y;
y = y+1;
y = tmp;
Run Code Online (Sandbox Code Playgroud)

如果你javac这个Y.java班:

public class Y {
    public static void main(String []args) {
        int y = 2;
        y = y++;
    }
}
Run Code Online (Sandbox Code Playgroud)

并且javap -c Y,您获得以下jvm代码(我允许我在Java虚拟机规范的帮助下评论main方法):

public class Y extends java.lang.Object{
public Y();
  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_2 // Push int constant `2` onto the operand stack. 

   1:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                 // value of the local variable at index `1` (`y`) to this value.

   2:   iload_1  // Push the value (`2`) of the local variable at index `1` (`y`)
                 // onto the operand stack

   3:   iinc  1, 1 // Sign-extend the constant value `1` to an int, and increment
                   // by this amount the local variable at index `1` (`y`)

   6:   istore_1 // Pop the value on top of the operand stack (`2`) and set the
                 // value of the local variable at index `1` (`y`) to this value.
   7:   return

}
Run Code Online (Sandbox Code Playgroud)

因此,我们终于有:

0,1: y=2
2: tmp=y
3: y=y+1
6: y=tmp
Run Code Online (Sandbox Code Playgroud)


Jer*_*ett 8

在 Java 中,x++ 和 ++x之间存在差异

++x 是一种前缀形式: 它增加变量表达式,然后在表达式中使用新值。

例如,如果在代码中使用:

int x = 3;

int y = ++x;
//Using ++x in the above is a two step operation.
//The first operation is to increment x, so x = 1 + 3 = 4
//The second operation is y = x so y = 4

System.out.println(y); //It will print out '4'
System.out.println(x); //It will print out '4'
Run Code Online (Sandbox Code Playgroud)

x++ 是一种后缀形式: 变量值首先在表达式中使用,然后在运算后递增。

例如,如果在代码中使用:

int x = 3;

int y = x++;
//Using x++ in the above is a two step operation.
//The first operation is y = x so y = 3
//The second operation is to increment x, so x = 1 + 3 = 4

System.out.println(y); //It will print out '3'
System.out.println(x); //It will print out '4' 
Run Code Online (Sandbox Code Playgroud)

希望这很清楚。运行和使用上面的代码应该有助于您的理解。


小智 7

在考虑计算机实际上做了什么......

++ x:从内存加载x,递增,使用,存储回内存.

x ++:从内存加载x,使用,增量,存储回内存.

考虑:a = 0 x = f(a ++)y = f(++ a)

函数f(p)返回p + 1

x将为1(或2)

y将是2(或1)

其中存在问题.编译器的作者是在检索之后,使用之后还是在存储之后传递参数.

通常,只需使用x = x + 1.这样更简单.