Java中=+和+=的区别?

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

Difference between =+ and += in Java?

java

提问by d0001

Can anyone explain what is happening when you use =+ ?

谁能解释一下当你使用 =+ 时发生了什么?

int one = 1 ;
int two = 2 ;

int sum1 = 0 ;
int sum2 = 0 ;

sum1 =+ one ;
sum2 += two ;

sum1 =+ two ;
sum2 += one ;

System.out.println(sum1) ;
System.out.println(sum2) ;

Output:

输出:

2
3

Why is 1st line 2?

为什么第一行是 2?

回答by Daniel Williams

Java doesn't care too much for white space. =+is being interpreted as =for assignment and +for the unary plus operator which doesn't have any effect here. It is a little used operator and you can read about exactly what it does here http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.15.3

Java 不太关心空白。=+被解释=为赋值和+一元加运算符,在这里没有任何影响。这是一个很少使用的运算符,您可以在这里阅读它的确切作用http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.15.3

You can read more about the different operators in Java here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

您可以在此处阅读有关 Java 中不同运算符的更多信息https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

回答by ΦXoc? ? Пepeúpa ツ

Doing this

这样做

sum1 += one ;

is the same as sum1 = (sum1_type)(sum1 + one);

是相同的 sum1 = (sum1_type)(sum1 + one);

and doing this

并这样做

sum2 =+ two ;

is the same as

是相同的

and doing this sum2 = two;(Unary plus operator; indicates positive value) and is not affecting the sign of variable two

并且这样做sum2 = two;(一元加运算符;表示正值)并且不影响变量的符号