Reputation: 207
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.
Upvotes: 2
Views: 258
Reputation: 36
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!
Upvotes: 0
Reputation: 5082
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.
Upvotes: 8
Reputation: 200148
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.
Upvotes: 0
Reputation: 310
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.
Upvotes: 0
Reputation: 2644
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()
Upvotes: 0
Reputation: 1677
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
Upvotes: 0
Reputation: 33046
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.
Upvotes: 2
Reputation: 4624
There's basically no difference. The +=
is only for your convenience.
Upvotes: 0
Reputation: 45060
There is actually no difference at all in both the statements.
b += a
internally will be treated as b = b + a
only.
Upvotes: 0
Reputation: 56697
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;
Upvotes: 3