3

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?

El3ctr0n1c4
  • 400
  • 2
  • 7
  • 18

9 Answers9

18

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

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
  • How is this different from the link provided previously by Mystical in the comments to the original question? – Hovercraft Full Of Eels Jan 07 '12 at 22:19
  • @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. – TacticalCoder Jan 07 '12 at 22:25
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.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
3

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

Evert
  • 8,161
  • 1
  • 15
  • 17
2

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.

Bruno
  • 119,590
  • 31
  • 270
  • 376
1

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
Davy8
  • 30,868
  • 25
  • 115
  • 173
  • See the answer below ( http://stackoverflow.com/questions/8773349/difference-between-a-b-and-a-a-b/8773408#8773408 ) for the explanation. – Rob W Jul 23 '12 at 10:00
1

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 (>>>=) .

Vladislav Zakatov
  • 146
  • 1
  • 1
  • 6
1

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.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
0

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

0

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476