Java Man
Java Man

Reputation: 163

What is the difference between += and =+?

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

Upvotes: 16

Views: 5159

Answers (9)

Anubhav Mishra
Anubhav Mishra

Reputation: 125

The += operation as you said, is used for increment by a specific value stated in the R value.Like,

i = i+1;
//is equivalent to 
i += 1;

Whereas, =+ is not any proper operation, its basically 2 different operators equal and unary plus operators written with each other.Infact the + sign after = makes no sense, so try not to use it.It will only result in a hocum.

i =+ 1;
//is equivalent to
i = +(1);

Upvotes: 0

takra
takra

Reputation: 467

When you have a+=b, that means you're adding b to whatever's already in a. If you're doing a=+b, however, you're assigning +b to a.

int a=2;
int b=5;
a+=b;
System.out.println(a); //Prints 7

a=2;
b=5;
a=+b;
System.out.println(a); //Prints 5

Upvotes: 1

kennytm
kennytm

Reputation: 523264

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://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.3 for what a unary + does.)

Upvotes: 33

Stephen C
Stephen C

Reputation: 718788

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".

Upvotes: 3

fastcodejava
fastcodejava

Reputation: 41097

+= 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.

Upvotes: 3

AnT stands with Russia
AnT stands with Russia

Reputation: 320421

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.

Upvotes: 2

Peter Tillemans
Peter Tillemans

Reputation: 35331

=+ 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.

Upvotes: 5

tvanfosson
tvanfosson

Reputation: 532435

+= 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.

Upvotes: 12

jasonmp85
jasonmp85

Reputation: 6817

+= 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

Upvotes: 8

Related Questions