Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

+= vs =+ in Java

I am familiar with the += operator in java, that if executed like:

i += 2;

is the same as:

i = i + 2;

But what exactly is =+? It compiles fine in Java, but as far as I can tell it only assigns the result right-hand-side expression to the left-hand-side variable. i.e.

i =+ 2

is the same as:

i = 2

Anyone know what exactly is going on here? Apologies if this has been asked, the SEO for any question regarding operators is always wonky... Thanks!

like image 547
Ruben Martinez Jr. Avatar asked Oct 06 '14 23:10

Ruben Martinez Jr.


2 Answers

i =+ 2 is the same as

i = +2;

i.e., the + is the unary + operator.

like image 128
T.C. Avatar answered Oct 24 '22 01:10

T.C.


i =+ 2 is equivalent to i = +2 where + is the unary plus operator

like image 3
Reimeus Avatar answered Oct 24 '22 01:10

Reimeus