Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the operators += vs =+

Tags:

I just realized that I was using =+ instead of the operator += and my program was doing all sorts of weird and unexpected things. Eclipse didn't give me an error of any kind so I assume that =+ is a legitimate operator but there is no reference to that in my book.

My question is what does =+ do if anything and under what circumstances would you use it?

like image 801
Jessica M. Avatar asked Jun 20 '13 06:06

Jessica M.


People also ask

What is the difference between '/' and '%' operators?

Solution. The / operator is used for division whereas % operator is used to find the remainder.

What is the difference between '/' and in Python?

In Python programming, you can perform division in two ways. The first one is Float Division("/") and the second is Integer Division("//") or Floor Division.

What is the difference between and || operator in C?

The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand. The conditional logical OR operator ||, also known as the "short−circuiting" logical OR operator, computes the logical OR of its operands.

What is the difference between == and === *?

The difference between == and === is that: == converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.


1 Answers

A common syntax is:

 += 

This is the add and assignment operator, which adds right-hand expression to the left-hand variable then assigns the result to left-hand variable. For example:

 int i = 1;  int j = 2;  i += j;   // Output: 3  System.out.println( i ) 

A far less common syntax is:

=+ 

Usually this is written as two different operators, separated by a space:

= + 

Without the space, it looks as follows:

int i = 1; int j = 2;      i =+ j;  // Output: 2 System.out.println(i); 

An idiomatic way to write this is to shift the unary operator to the right-hand side:

int i = 1; int j = 2;      i = +j;  // Output: 2 System.out.println(i); 

Now it's easy to see that i is being assigned to the positive value of j. However, + is superfluous, so it's often dropped, resulting in i = j, effectively the equivalent of i = +1 * j. In contrast is the negative unary operator:

int i = 1; int j = 2;      i = -j;  // Output: -2 System.out.println(i); 

Here, the - would be necessary because it inverts the signedness of j, effectively the equivalent of i = -1 * j.

See the operators tutorial for more details.

like image 68
Suresh Atta Avatar answered Sep 18 '22 15:09

Suresh Atta