2

I often use both forms: short and long of addition (also subtraction, multiplication, etc.) operator in java. And I thought that this doesn't impact to performance or speed, but I was confused by questions: "Why java creators provided two forms of this operators? And what is the difference between them?" So, what's real difference between two forms:

int a = 10, b = 3;
b = b + a;

and

int a = 10, b = 3;
b += a;

Can someone explain me this? May be difference between two forms is hidden at a lower level? Every book says only: "Java also has compound operators..." but nothing about difference.

Tuna
  • 2,937
  • 4
  • 37
  • 61

11 Answers11

8

Consider the following code:

int x = 9;
short s = 2;
s = s+x; // Compiler error
s += x;  // Compiles 

So, basically when you say: s += x it means that s = (short)(s+x). And when you use s = s+x, the compiler will complain since, it cannot cast x from int to short implicitly. So, += operator takes care of typecasting.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
3

It doesn't say anything about the difference because there is none from the programmer's point of view. The

b = c + a;

notation exists because it is the normal case of an addition. The short form

b += a;

exists for the special case that c "is" b and really expands to

b = b + a;
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 2
    Note that, as an example, if b is a `byte` and a is an `int`. `b = b+a;` won't compile but `b+=a;` will compile because it's equivalent to `b = (byte)(b+a);`. There is always an implicit cast when you use a compound operator. Here in this case, `b+=a` is equivalent to `(int)(b+a);` and not `b = b+a;` – Alexis C. May 08 '13 at 09:51
2

The difference is that in the first case, a temporary object c = (a + b) is created, then assigned back to b. In the second case, the operation happens in place and should be more efficient.

At least, this used to be in old C++: modern compilers and JIT will automatically detect and optimize this, so, actually, there is no difference at all.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
1

One is shorter, that's about it.

Rik
  • 28,507
  • 14
  • 48
  • 67
0

There is actually no difference at all in both the statements.

b += a internally will be treated as b = b + a only.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

There's basically no difference. The += is only for your convenience.

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
0

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand B += A is equivalent to B = B + A

Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
0

So far i only found a difference with the logical compound operators:

boolean accepted = firstCheck();

version 1: accepted = accepted && secondCheck();
version 2: accepted &= secondCheck();

the above code will behave differently:

version 1 will omit calling secondCheck() if accepted is false

version 2 will always call secondCheck()

Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
0

Theres is no difference between them hidden on lower level. b += a is equivalent to b = b + a. However, there's some danger in here. It may seem that b += a is an atomic operation, however it first reads b, then reads a, then sums them, and then writes the result to b. Treating it as an atomic operation may cause errors when dealing with concurrent operations. In other words, always rememeber to provide some explicit lock when using this shorter += operator in concurrent environment.

Dale Cooper
  • 310
  • 2
  • 9
0

Another way to ask yourself the same question would be, What is the difference between java.lang.String and just String? The difference is huge in terms of what source code is all about: expressiveness and readability.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

This question is really very old. But I want to summarize: I have found in the Java Language specification the next text:

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.

So, it's enought for understanding!

Vadim
  • 36
  • 5