Java 中的后增量 (i++) 和前增量 (++i) 运算符如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2371118/
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
How do the post increment (i++) and pre increment (++i) operators work in Java?
提问by Ankit Sachan
Can you explain to me the output of this Java code?
你能向我解释一下这个 Java 代码的输出吗?
int a=5,i;
i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;
System.out.println(a);
System.out.println(i);
The output is 20 in both cases
两种情况下的输出都是 20
采纳答案by kgiannakakis
Does this help?
这有帮助吗?
a = 5;
i=++a + ++a + a++; =>
i=6 + 7 + 7; (a=8)
a = 5;
i=a++ + ++a + ++a; =>
i=5 + 7 + 8; (a=8)
The main point is that ++a
increments the value and immediately returns it.
要点是++a
增加值并立即返回它。
a++
also increments the value (in the background) but returns unchanged value of the variable - what looks like it is executed later.
a++
还会增加值(在后台)但返回变量的未更改值 - 看起来它稍后执行。
回答by Aurril
++a
increments a
before it is evaluated.
a++
evaluates a
and then increments it.
++a
a
在评估之前递增。
a++
计算a
然后增加它。
Related to your expression given:
与您给出的表达式相关:
i = ((++a) + (++a) + (a++)) == ((6) + (7) + (7)); // a is 8 at the end
i = ((a++) + (++a) + (++a)) == ((5) + (7) + (8)); // a is 8 at the end
The parenteses I used above are implicitly used by Java. If you look at the terms this way you can easily see, that they are both the same as they are commutative.
我上面使用的括号是 Java 隐式使用的。如果您以这种方式查看这些术语,您可以很容易地看到,它们都是相同的,因为它们是可交换的。
回答by codaddict
i = ++a + ++a + a++;
is
是
i = 6 + 7 + 7
Working: increment a to 6 (current value 6) + increment a to 7 (current value 7). Sum is 13 now add it to current value of a (=7) and then increment a to 8. Sum is 20 and value of a after the assignment completes is 8.
工作:将 a 增加到 6(当前值 6)+ 将 a 增加到 7(当前值 7)。Sum 为 13 现在将其添加到 a (=7) 的当前值,然后将 a 增加到 8。Sum 为 20,赋值完成后 a 的值为 8。
i = a++ + ++a + ++a;
is
是
i = 5 + 7 + 8
Working: At the start value of a is 5. Use it in the addition and then increment it to 6 (current value 6). Increment a from current value 6 to 7 to get other operand of +. Sum is 12 and current value of a is 7. Next increment a from 7 to 8 (current value = 8) and add it to previous sum 12 to get 20.
工作: a 的起始值为 5。在加法中使用它,然后将其增加到 6(当前值 6)。将 a 从当前值 6 增加到 7 以获得 + 的其他操作数。总和为 12,a 的当前值为 7。接下来将 a 从 7 增加到 8(当前值 = 8)并将其添加到之前的总和 12 中得到 20。
回答by Thorbj?rn Ravn Andersen
when a
is 5, then a++
gives a 5 to the expression and increments a
afterwards, while ++a
increments a
before passing the number to the expression (which gives a
6 to the expression in this case).
whena
是 5,然后a++
给表达式一个 5 并在a
之后递增,而在将数字传递给表达式之前++a
递增a
(a
在这种情况下为表达式给出6)。
So you calculate
所以你计算
i = 6 + 7 + 7
i = 5 + 7 + 8
回答by Lombo
++a
increments and then uses the variable.a++
uses and then increments the variable.
++a
递增,然后使用该变量。a++
使用然后增加变量。
If you have
如果你有
a = 1;
and you do
你也是
System.out.println(a++); //You will see 1
//Now a is 2
System.out.println(++a); //You will see 3
codaddict explainsyour particular snippet.
codacci 解释了您的特定片段。
回答by vinod
In the above example
在上面的例子中
int a = 5,i;
i=++a + ++a + a++; //Ans: i = 6 + 7 + 7 = 20 then a = 8
i=a++ + ++a + ++a; //Ans: i = 8 + 10 + 11 = 29 then a = 11
a=++a + ++a + a++; //Ans: a = 12 + 13 + 13 = 38
System.out.println(a); //Ans: a = 38
System.out.println(i); //Ans: i = 29
回答by Vineet Sahu
a=5; i=++a + ++a + a++;
is
是
i = 7 + 6 + 7
Working: pre/post increment has "right to left" Associativity , and pre has precedence over post , so first of all pre increment will be solve as (++a + ++a) => 7 + 6
. then a=7
is provided to post increment => 7 + 6 + 7 =20
and a =8
.
工作:前/后增量具有“从右到左”的关联性,并且 pre 优先于 post ,因此首先 pre 增量将被解决为(++a + ++a) => 7 + 6
。thena=7
用于发布增量 =>7 + 6 + 7 =20
和a =8
.
a=5; i=a++ + ++a + ++a;
is
是
i=7 + 7 + 6
Working: pre/post increment has "right to left" Associativity , and pre has precedence over post , so first of all pre increment will be solve as (++a + ++a) => 7 + 6
.then a=7
is provided to post increment => 7 + 7 + 6 =20
and a =8
.
工作:前/后增量具有“从右到左”关联性,并且 pre 优先于 post ,因此首先将解决预增量,因为(++a + ++a) => 7 + 6
.thena=7
提供给后增量 =>7 + 7 + 6 =20
和a =8
。
回答by Java Main
pre-increment and post increment are equivalent if not in an expression
如果不在表达式中,则前增量和后增量是等效的
int j =0;
int r=0
for(int v = 0; v<10; ++v) {
++r;
j++;
System.out.println(j+" "+r);
}
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
回答by Rishabh Vashishtha
I believe however if you combine all of your statements and run it in Java 8.1 you will get a different answer, at least that's what my experience says.
但是我相信,如果您将所有语句组合起来并在 Java 8.1 中运行它,您会得到不同的答案,至少我的经验是这样说的。
The code will work like this:
代码将像这样工作:
int a=5,i;
i=++a + ++a + a++; /*a = 5;
i=++a + ++a + a++; =>
i=6 + 7 + 7; (a=8); i=20;*/
i=a++ + ++a + ++a; /*a = 5;
i=a++ + ++a + ++a; =>
i=8 + 10 + 11; (a=11); i=29;*/
a=++a + ++a + a++; /*a=5;
a=++a + ++a + a++; =>
a=12 + 13 + 13; a=38;*/
System.out.println(a); //output: 38
System.out.println(i); //output: 29
回答by Xinyi Liu
++a is prefix increment operator:
++a 是前缀增量运算符:
- the result is calculated and stored first,
- then the variable is used.
- 先计算并存储结果,
- 然后使用该变量。
a++ is postfix increment operator:
a++ 是后缀增量运算符:
- the variable is used first,
- then the result is calculated and stored.
- 首先使用变量,
- 然后计算并存储结果。
Once you remember the rules, EZ for ya to calculate everything!
一旦你记住了规则,EZ为你计算一切!