Java equals plus operator

user1610458 picture user1610458 · Feb 24, 2015 · Viewed 7.2k times · Source

I need to explain this strange operator =+ (equal plus)

Example #1:

Double a = new Double(5);
Double b = new Double(10);
a += b

result:

a=15.0
b=10.0

Example #2:

Double a = new Double(5);
Double b = new Double(10);
a =+ b

result:

a=10.0
b=10.0

I understand the first example, but please explain me what this =+ operator did in example no.2.

And another interesting fact is, that these operators are valid and compilable: +=, -=, *=, /=
but any of these two won't compile: =*, =/

Answer

Konstantin Yovkov picture Konstantin Yovkov · Feb 24, 2015

=+ is the assignment operation and the unary + afterwards. It's perfectly valid and what happens is:

a = (+b); 

It's pretty much the same when you want to assign the negative value of a variable to another variable:

a = (-b); //a will be assigned with -10

Also, =* doesn't compile, because there's no * unary operator.