difference between a += b and a = a + b
Solution 1
See the Java language specification, 15.26.2 Compound assignment operators
To quote the relevant parts:
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
short x = 3;
x += 4.6;
and results in x having the value 7 because it is equivalent to:
short x = 3;
x = (short)(x + 4.6);
So it is more than syntactic sugar, as
int x = 1;
long a = 2l;
x+= a;
compiles, where
int x =1;
long a =2l;
x = x+a;
gives you a compile error, as was discussed here on StackOverflow quite recently
Solution 2
it does depend on the language, but in c# it is very slightly more efficient to use a += b;.
a is only evaluated once.
in a = a + b, a is evaluated twice.
http://msdn.microsoft.com/en-us/library/sa7629ew.aspx
Solution 3
Just syntactic sugar in most languages that I know that would include c, c++, C#, java, javascript..
notable difference noted by Cicada in regards to c++:
On numeric types (int and friends) there is no difference. On user-defined classes there may be a difference. A notable one would be D3DXVECTOR3 from DirectX, for example. Using + would construct a temporary object while += would not. The latter is about 30% faster.
Solution 4
In most languages that support this notation, a = a + b
is the same as a += b
, but it's not always the case.
Here is an example in Python (using Numpy):
>>> import numpy as np
>>> a = np.array([1])
>>> b = np.array([2])
>>> c = a
>>> a = a + b
>>> print a
[3]
>>> print c
[1]
Here a = a + b
creates a new array for a + b
and stores it into a
. c
, which was using the same reference as the initial a
still holds the initial array (with value 1).
>>> a = np.array([1])
>>> b = np.array([2])
>>> c = a
>>> a += b
>>> print a
[3]
>>> print c
[3]
Here a += b
re-uses the initial array a
. As a result, since both a
and c
refer to the same array, both a
and c
are modified.
Solution 5
In addition to everything said above, you can also use the following shortcuts:
Operator (+=)
x+=y;
same as:
x=x+y;
Operator (-=)
x-=y;
same as:
x=x-y;
Operator (*=)
x*=y;
same as:
x=x*y;
Operator (/=)
x/=y;
same as:
x=x/y;
Operator (%=)
x%=y;
same as :
x=x%y;
Operator (&=)
x&=y;
same as :
x=x&y;
Operator (|=)
x|=y;
same as :
x=x|y;
Operator (^=)
x^=y;
same as :
x=x^y;
Operator (>>=)
x>>=y;
same as
result=x>>y;
The same operation to operator (<<=) and operator (>>>=) .
El3ctr0n1c4
Updated on June 08, 2022Comments
-
El3ctr0n1c4 almost 2 years
In Java, I really wonder is there a difference between using
a += b;
ora = a + b;
. Which one should I use principally? I know that first one is something like shortcut but does the compiler get those two indications differently?-
Mark Rolich over 12 yearsPlease specify language.
-
Bruno over 12 yearsWhich language? In which context? Depending on the language, it may also depend on the type of
a
andb
. -
Hovercraft Full Of Eels over 12 yearslook up "sytactic sugar".
-
Mysticial over 12 years
-
Jeremy D over 12 yearsThe a+=b thing is useful to make more concise instruction but not necessarily the easiest to understand! That depends only of you I guess.
-
El3ctr0n1c4 over 12 years@Mysticial I've just checked the link. "wow". those were really different. Thanks
-
-
user703016 over 12 yearsIt's not syntactic sugar in C++.
-
Rogel Garcia over 12 yearsCan an overridden operator in C++ be different? That is, override + in one way and += other way?
-
Ry- over 12 years@rogelware: Yes. (And I wish it couldn't be.)
-
Bassam Mehanni over 12 years@Cicada it has been a while since I used C++, could you please tell me what the difference is between a += b; and a = a + b;?
-
Bassam Mehanni over 12 yearsyes if it's overridden but in its default state, isn't it essentially the same, or am I missing something?
-
user703016 over 12 years@BassamMehanni On numeric types (int and friends) there is no difference. On user-defined classes there may be a difference. A notable one would be D3DXVECTOR3 from DirectX, for example. Using
+
would construct a temporary object while+=
would not. The latter is about 30% faster. -
Bassam Mehanni over 12 years@Cicada Thanks, do you mind if I added this to the answer for completeness?
-
user703016 over 12 years@BassamMehanni Feel free to do so.
-
Hovercraft Full Of Eels over 12 yearsHow is this different from the link provided previously by Mystical in the comments to the original question?
-
TacticalCoder over 12 years@HovercraftFullOfEels: I'd say it's different in that this answer specifies, before any edit, that there's more to it than syntactic sugar as has been discussed extensively here a few days ago. And he did provide the link to one these recent questions.
-
Rob W almost 12 yearsSee the answer below ( stackoverflow.com/questions/8773349/… ) for the explanation.