views:

355

answers:

7

What is the difference between += and =+? Specifically, in java, but in general also.

+19  A: 
i += 4;

means

i = i + 4;  // increase i by 4.

While

i =+ 4;

is equivalent to

i = +4;   // assign 4 to i. the unary plus is effectively no-op.

(See http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#24926 for what a unary + does.)

KennyTM
Occasionally the unary plus can add a bit of clarity. It's clear that 5 means positive 5, but does 0.0 mean positive or negative zero (there are both in IEEE 754). So writing +0.0 can help in the rare case that it matters.
jasonmp85
just curious.. what would +(-4) be taken as?
Ram Bhat
@jasonmp8.5: But if you just ask for 0.0, you'll get the positive version.
Donal Fellows
@Ram: -4. See update.
KennyTM
@jasonmp85 - even when the unary plus adds value, it should be associated with the constant, not with the assignment operator.
tvanfosson
@donal-fellows: this it true, but if you see '0.0' in code, does it mean the author wanted positive zero, or that they just wrote 0.0 and by chance the algorithm did the right thing? '+0.0' is explicit: they *meant* positive zero.
jasonmp85
@jason: You are aware that positive and negative zero are equal in all operations (except when you divide a finite non-zero number by them)?
Donal Fellows
They may be equal in all aspects, but do not behave equally in all operations: http://en.wikipedia.org/wiki/Signed_zero. I'm not going to make any presumptions about how people want to use the sign bit. I'm sure negative zero has allowed many perverse algorithms throughout the years.
jasonmp85
+6  A: 

+= is get and increment:

a += 5; // adds 5 to the value of a

=+ isn't really a valid identifier on its own, but might show up when you're using the unary + operator:

a =+ 5; // assigns positive five to a
jasonmp85
Of course no one would *ever* write the second example, since the unary plus operator binds with the `5` and has no business getting all cozy with the `=` operator like that. It's just shameful, is what it is.
jasonmp85
+7  A: 

+= is an operator that increments the left-hand side of the assignment by the value of the right-hand side and assigns it back to the variable on the left-hand side. =+ is not an operator but, in fact, two operators: the assignment operator = and the unary plus + (positive) operator which denotes the value on the right-hand side is positive. It's actually redundant because values are positive unless they are negated with unary minus. You should avoid the =+ construct as it's more likely to cause confusion than do any actual good.

tvanfosson
+4  A: 

=+ is not an operator. The + is part of the number following the assignment operator.

int a = 4; int b = 4;

a += 1; b =+1;

System.out.println("a=" + a + ", b=" + b);

This shows how important it is to properly format your code to show intent.

Peter Tillemans
+2  A: 

Specifically, in java, but in general also.

In Java x += <expr>; is equivalent to x = x + ( <expr> ); where the + operator may be the arithmetical add operator or the string concatenation operator, depending on the type of x. On the other hand, x =+ <expr>; is really an ugly way of writing x = + <expr>; where the + is unary plus operator ... i.e. a no-op for numeric types and a compilation error otherwise.

The question is not answerable in the general case. Some languages support a "+=" operator, and others don't. Similarly, some languages might support a "=+" operator and others won't. And some languages may allow an application to "overload" one or other of the operators. It simply makes no sense to ask what an operator means "in general".

Stephen C
+1  A: 

I don't know what you mean by "in general", but in the early versions of C language (which is where most of Java syntax came from, through C++), =+ was the original syntax for what later became +=, i.e. i =+ 4 was equivalent to i = i + 4.

CRM (C Reference Manual) is the document the describes C language with =+, =-, =>> and so on.

AndreyT
+3  A: 

+= is a way to increment numbers or String in java. E.g.

int i = 17;
i += 10;  // i becomes 27 now.

There is no =+ operator. But if you do i =+ 10; it means i is equal to +10 which is equal to just 10.

fastcodejava