What is the difference using the operators `+=` vs `=+` in Java?
Asked Answered
C

6

23

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 and under what circumstances would you use it?

Confusion answered 20/6, 2013 at 6:44 Comment(0)
B
20

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.

Bootjack answered 20/6, 2013 at 6:46 Comment(0)
H
16

=+ is the same as the assignment operator =

a =+ b;

is equivalent to

a = (+b);

which is the same as

a = b;
Homochromatic answered 20/6, 2013 at 6:52 Comment(1)
Indeed, given that a=-1; is a valid line I think it was decided that a=+1 should also be valid despite doing nothing. I sometimes use it to emphasis some intentAtthia
B
6

The important fact to highlight is that there is no =+ assignment operator in Java.

You're just listing two operators, one after the other (an = then a +)

  • i =+ j; is just like writing i = +j;.

While a little funky looking at it that way, it's very clear to everyone when considering the minus sign.

  • i =- j; is the same as i = -j;

As mentioned in many other answers, the Add and Assignment operator +=

  • Adds the right hand side expression to the value of the left hand variable
  • Then assigns the value of that addition to the left hand variable.
Bilharziasis answered 19/3, 2018 at 13:43 Comment(0)
D
3

+= adds the right operand to left operand and assign it to left operand

=+ assign a value to the left operand

Dished answered 20/6, 2013 at 6:52 Comment(0)
F
1

Actually saying that i=j is the same as i= +j is not totally true, because i= +j promotes the variable type to int if it's smaller than int, while assignment does not. For example:

byte b1 = 2;
short s1 = b; // compiles and s1 equals 2 of type short
short s2 = +b; // Compile error: required type: short, provided int
// Because the result of +b equals 2 of type int
Farrel answered 11/4, 2021 at 15:56 Comment(0)
S
0

There is no effect of =+ other than that it is same as =

Serous answered 20/6, 2013 at 19:39 Comment(1)
Do not use answers to ask for help; use comments, if you want to add a comment, or just open a new question.Omnipotence

© 2022 - 2024 — McMap. All rights reserved.