Reputation: 400
In Java, I really wonder is there a difference between using a += b;
or a = 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?
Upvotes: 3
Views: 13126
Reputation: 14944
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.
Upvotes: 3
Reputation: 16781
I'ld prefer a += b
over a = a + b
. First of all it is less to write, second it is more clear what is going on. And when talking about C++ and classes it might be more efficient using +=. Take a look at this sample:
class C {
public:
const C &operator =(const C &rhs) { printf("=\n"); x = rhs.x; return *this; }
C operator +(const C &rhs) { printf("+\n"); C c; c.x = x + rhs.x; return c; }
C &operator +=(const C &rhs) { printf("+=\n"); x += rhs.x; return *this; }
int x;
};
C a, b;
a.x = 1;
b.x = 2;
a += b; // same as a.operator+=(b)
a = a + b; // same as a.operator=(a.operator+(b))
As you can see with operator +=
you have less temporary objects and better performance.
Upvotes: 1
Reputation: 146
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 (>>>=) .
Upvotes: 1
Reputation: 36621
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
Upvotes: 18
Reputation: 8541
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
Upvotes: 3
Reputation: 122749
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.
Upvotes: 2
Reputation: 31606
In most cases it ends up being the same thing.
In some languages +=
is a separate operator that can be overloaded to do something different.
For instance in Python with lists, the behavior is different (I learned this the hard way)
a = [1]
b = [2]
z = a
a = a + b
#z is not modified
a = [1]
b = [2]
z = a
a += b
# z is modified
Upvotes: 1
Reputation: 225273
No, the compiler doesn't differentiate between the two in output. The only difference is in parsing and possibly in lexical analysis (depending on expansion). You would generally use the shortcut form +=
. It's just syntactic sugar.
Upvotes: 0
Reputation:
It's compiler- (and so implementation-) defined whether the compiler generates different code from the above sources, but one thing is sure: a += b is supposed to be a simpler short-hand notation for a = a + b. (that's why, in general, a sane compiler would generate the same code for both).
Upvotes: 0