Can you clearly explain the difference between the operator += and the operator =+ ? Obviously, both are shortcuts for a sum, but I don't get the meaning of "=+"
a += b is equivalent to a = a + b. But what is the equivalence of a =+ b ???
Here is the practical example:
public class SumOfSquares {
private int[] inputArray;
private Integer result;
public SumOfSquares(int[] inputArray) {
this.inputArray=inputArray;
result = new Integer(0);
}
public Integer getResult () {
for (int counter=0; counter<inputArray.length; counter++) {
int currentNumber = inputArray[counter];
result += currentNumber*currentNumber;
}
return result;
}
}
inputArray={1,2,3,4,5}. Expected result=55 (1^2+2^2+3^2+4^2+5^2 = 1+4+9+16+25 = 55)
If I replace result += currentNumber*currentNumber; by result =+ currentNumber*currentNumber;, I get a result of 25 instead of 55. I would like to understand why.
=+ is not an operator. You might be confusing it with the combination of the assignment = and the unary + operator, which will take the value as positive (doesn't change its sign, + (-3) is still -3) and can be perfectly ommitted for integer values.
int a = 5;
int b = 3;
a = (+b); // a = 3
a = (-b); // a = -3
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
a=+b is the same as a=0+b, in other words, a=b
=+ is not an operator. it is the assignment operator =, followed by a positive sign +. The + is applied to the variable to the right, so you can read it as a= (+b).
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