java java中的前增量/后增量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11629136/
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-10-31 05:51:19  来源:igfitidea点击:

preincrement / postincrement in java

java

提问by Mr. Ghiandino

Can someome help me to understand why:

有人可以帮我理解为什么:

int i=1;
int j=1;
int k=1;
int l=1;

System.out.println(i++ + i++);  
System.out.println(++j + ++j);  
System.out.println(k++ + ++k);  
System.out.println(++l + l++);

give:

给:

3
5
4
4

3
5
4
4

回答by ATaylor

Variable++ means: Increment variable AFTER evaluating the expression.

Variable++ 的意思是:在计算表达式后增加变量。

++Variable means: Increment variable BEFORE evaluating the expression.

++Variable 表示:在评估表达式之前增加变量。

That means, to translate your example to numbers:

这意味着,将您的示例转换为数字:

System.out.println(i++ + i++);  //1 + 2
System.out.println(++j + ++j);  //2 + 3
System.out.println(k++ + ++k);  //1 + 3
System.out.println(++l + l++);  //2 + 2

Does this clear things up, or do you need further explanations?

这是否澄清了问题,还是您需要进一步解释?

To be noted: The value of all those variables after the 'println' equal '3'.

需要注意的是:'println' 之后的所有变量的值等于'3'。

Since the OP asked, here's a little 'use-case', on where this behaviour is actually useful.

由于 OP 询问,这里有一个小“用例”,说明此行为实际上有用的地方。

int i = 0;
while(++i < 5) {           //Checks 1 < 5, 2 < 5, 3 < 5, 4 < 5, 5 < 5 -> break. Four runs
    System.out.println(i); //Outputs 1, 2, 3, 4 (not 5) 
}

Compared to:

相比:

int i = 0;
while(i++ < 5) {           //Checks 0 < 5, 1 < 5, 2 < 5, 3 < 5, 4 < 5, 5 < 5 -> break. Five runs
    System.out.println(i); //Outputs 1, 2, 3, 4, 5
}

回答by Shankhoneer Chakrovarty

Things to know:
1. Java evaluates expressions from left to right
2. ++i-pre-increment i.e increment before assignment
3. i++ - post increment i.e. increment after assignment

须知:
1. Java 从左到右计算表达式
2. ++i-pre-increment 即赋值前递增
3. i++ - post increment 即赋值后递增

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

op1=i++
op2=1++
sum=op1+op2
i++ - post increment the value of i

op1=i++
op2=1++
sum=op1+op2
i++ - 增加 i 的值

  1. Assign i to op1 and then increment the value of i.op1=1,i=2
  2. Assign i to op2 and then increment the value of i.op2=2,i=3
  3. Sum=3

    System.out.println(++j + ++j);

  1. 将 i 分配给 op1,然后增加 i.op1=1,i=2 的值
  2. 将 i 分配给 op2,然后增加 i.op2=2,i=3 的值
  3. 总和=3

    System.out.println(++j + ++j);

op1=++j
op2=++j
sum=op1+op2
++i - Pre increment the value of i

op1=++j
op2=++j
sum=op1+op2
++i - 预增 i 的值

  1. now at first j will be incremented to 2 and then assigned to op1.
  2. Then, j will again be incremented to 3 and assigned to op2
  3. Then, op1 and op2 will be added to print the sum as 5 and the value of j will be

    System.out.println(k++ + ++k);

  1. 现在首先 j 将增加到 2,然后分配给 op1。
  2. 然后,j 将再次增加到 3 并分配给 op2
  3. 然后,将添加 op1 和 op2 以将总和打印为 5,j 的值将是

    System.out.println(k++ + ++k);

op1=k++
op2=++k
sum=op1+op2

op1=k++
op2=++k
sum=op1+op2

  1. Assign the value of k to op1 and then increment k. op1=1,k=2

  2. Increment the value of k and then assign to op2. op2=3,k=3

  3. Sum = 4

    System.out.println(++l + l++);

  1. 将 k 的值赋给 op1,然后递增 k。op1=1,k=2

  2. 增加 k 的值,然后分配给 op2。op2=3,k=3

  3. 总和 = 4

    System.out.println(++l + l++);

Apply the above logic here also.

也在这里应用上述逻辑。

回答by mmey

As the name indicates, a post increment increments the value of the variable AFTER the variable has been processed (read) while the pre incrment increments the value BEFORE.

顾名思义,后增量在变量被处理(读取)之后增加变量的值,而前增量在之前增加值。

For i, that means that first i is incremented by 1, but read as 1, then incremented by 1 again (already being 2 now from the first increment), thus incremented to 3, but read as 2. This results in 1+2 = 3 and the value of i will be 3 as well...

对于 i,这意味着首先 i 增加 1,但读取为 1,然后再次增加 1(从第一次增加到现在已经是 2),因此增加到 3,但读取为 2。这导致 1+2 = 3 并且 i 的值也将是 3...

回答by Daniel Persson

i++ + i++

means use i, then increment, so i is pushed to some kind of stack,

意味着使用 i,然后递增,所以 i 被推送到某种堆栈,

then increased by 1,

然后增加1,

then the operator (+) is pushed to the stack,

然后运算符 (+) 被压入堆栈,

then i (now 2) is pushed to the stack.

然后我(现在是 2)被推入堆栈。

Since the expresseion is now over, the values and operator are popped: the 2nd i is 2, the first i is 1, 2+1=3 (i is now 3, since it was incremented after being pushed).

由于表达式现在结束,值和运算符被弹出:第二个 i 是 2,第一个 i 是 1,2+1=3(i 现在是 3,因为它在被压入后增加了)。

The thing you are probably missing is that i isn't increased after the evaluation of the whole expression, in the case of a postincrement, and vice versa for preincrement.

您可能缺少的一点是,在对整个表达式求值后, i 没有增加,在后增量的情况下,反之亦然对于前增量。

回答by java_xof

System.out.println(i++)

System.out.println(i++)

-> produces 1 since it increments after printing, but when call it twice u will have 1 + 2 so u can translate it to

-> 产生 1,因为它在打印后递增,但是当调用它两次时,您将得到 1 + 2,因此您可以将其转换为

System.out.println(int i=i+1, plus int i = i + 1 -> this gives 2)

System.out.println(int i=i+1, plus int i = i + 1 -> 这给出了 2)

fist phrase gives 2 and second gives 3 but u print it before they i++ increments , so u will have 1 + 2 at the end

第一个短语给出 2,第二个给出 3 但你在它们 i++ 递增之前打印它,所以你最后会有 1 + 2

System.out.println(++j);

System.out.println(++j);

-> produces 2 since it increments before printing

-> 产生 2 因为它在打印前递增

So when u will have ++j = 2 and then ++j = 3 so ++j and ++j is now 5

所以当你有 ++j = 2 然后 ++j = 3 所以 ++j 和 ++j 现在是 5