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?
Doing this
sum1 += one ;
is the same as 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
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
You can read more about the different operators in Java here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With