本文介绍了Java中的n ++ VS ++ n之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java老师说,最好使用++ n而不是n ++,我看不出其背后的逻辑.有人知道吗?

My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?

推荐答案

++ n 会增加值并返回新值.

++n increments the value and returns the new one.

n ++ 会增加值并返回 old 一个.

n++ increments the value and returns the old one.

因此, 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 ++ 的返回值,它们也是相同的,尽管我不知道Java会这样做.

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.

这篇关于Java中的n ++ VS ++ n之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:30
查看更多