我想知道后修复运算符比赋值运算符更好的方式。换句话说,使用一种方法优于另一种方法的优点/局限性是什么。
int a = 10;
a++;
//over
int a = 10;
a += 1;
谢谢。
首先,a++
anda--
比a += 1
and更容易编写a -= 1
。
另外,假设您要执行一个方法并将其递增a
1。您的方法头:public static void doSomething(int smth)
然后,您可以执行几项操作:(假设这些行是您的main方法的一部分, int a = 10;
您可以使用后缀运算符:
doSomething(a++);
//this will at first execute your method with a and then increment it by one
或者您可以使用更长的版本
doSomething(a);
a += 1; //possible too, but longer
还有,--a
并且++a
将首先增加a,然后将其移交给方法或对其进行其他处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句