java Java中n++ VS ++n的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4752761/
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
The difference between n++ VS ++n in Java
提问by Mohammad
My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?
我的 Java 老师说最好使用 ++n 而不是 n++,我没有看到这背后的逻辑。有人知道吗?
回答by Herms
++n
increments the value and returns the new one.
++n
增加值并返回新的值。
n++
increments the value and returns the oldone.
n++
增加值并返回旧值。
Thus, n++
requires extra storage, as it has to keep track of the old value so it can return it after doing the increment.
因此,n++
需要额外的存储空间,因为它必须跟踪旧值,以便在执行增量后返回它。
I would expect the actual difference between these two to be negligible these days. I know a lot of compilers will optimize it so they're identical if the return of n++
isn't actually used, though I don't know of Java does that.
我希望现在这两者之间的实际差异可以忽略不计。我知道很多编译器会优化它,所以如果n++
实际上没有使用return ,它们是相同的,尽管我不知道 Java 会这样做。
回答by Mchl
Your Java teacher is (probably) refering to the fact that preincrementation is usuallya little tiny bitfaster than postincrementation. I'm not even sure if that's the case in Java, because I learned about that in C course.
您的Java老师是(可能)指的事实,preincrementation是平时一点点点点比postincrementation更快。我什至不确定 Java 是否是这种情况,因为我是在 C 课程中了解到的。
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"
Donald Knuth
“我们应该忘记小效率,比如大约 97% 的时间:过早的优化是万恶之源”
唐纳德·克努斯
In everyday pracitice, I would use pre- or postincrementation basing mainly on what makes the most sense in the code.
在日常实践中,我会根据代码中最有意义的内容来使用前增量或后增量。
回答by Bnjmn
n++ will increment the variable prior to the operation taking place, while ++n only increments the variable after the operation.
n++ 将在操作发生之前递增变量,而 ++n 仅在操作之后递增变量。
So take x = 1
y = x++
y = 1
x = 2 now
So take x = 1 again
y = ++x
y = 2
x = 2 now
回答by Rafe Kettler
Example:
例子:
int n = 10;
int a = n++; // a is 10
int b = n; // b is 11
int c = ++n; // c is 12
The difference is that the value of ++n
is n + 1
and the value of n++
is n
.
不同的是,价值++n
是n + 1
和值n++
就是n
。
Neither is better, they do the same thing. You shouldn't really rely on the difference between the two in your programs because 1.) quite a few people don't understand the difference and 2.) the same thing can be accomplished far more simply.
两者都不是更好,他们做同样的事情。你不应该在你的程序中真正依赖两者之间的区别,因为 1.) 很多人不理解区别 2.) 同样的事情可以更简单地完成。
回答by ktm5124
They do different things.
他们做不同的事情。
Let's say n = 10. Then n++ increments n but returns 10.
假设 n = 10。然后 n++ 增加 n 但返回 10。
++n increments n and returns 11.
++n 增加 n 并返回 11。
Though I'm not sure which one requires more assembly instructions.
虽然我不确定哪一个需要更多的组装说明。
回答by Elijah Saounkine
++n adds one to n, then uses it; n++ uses it then adds one. Same applies to --.
++n 将 n 加一,然后使用它;n++ 使用它然后添加一个。同样适用于--。
回答by Paul Sasik
Interesting example that illustrates the difference:
有趣的例子说明了差异:
public class Main {
public static void main(String[] args) {
for (int i=0; i<5; /*not incrementing here*/ )
System.out.println(i++);
}
}
output: 0 1 2 3 4
输出:0 1 2 3 4
public class Main {
public static void main(String[] args) {
for (int i=0; i<5; /*not incrementing here*/ )
System.out.println(++i);
}
}
output: 1 2 3 4 5
输出:1 2 3 4 5
Notice that neither for-loop performs the incrementation since it does not matter there.
请注意,这两个 for 循环都不执行增量,因为它在那里无关紧要。