I know that both this statements evaluate to same answer but is there any performance issue related to this statements?
Asked
Active
Viewed 129 times
0
-
1Nothing, a+=1 will be converted as a = a+1 internally by compiler, I guess. – kosa Jun 28 '13 at 03:38
-
1Look at the generated code. – Hovercraft Full Of Eels Jun 28 '13 at 03:38
-
The only difference is if `a` is an expression that has side-effects when evaluated. In the former case it gets evaluated (has side-effects) twice, in the latter case only once. If it is just a simple variable name for example they are identical 100%. – Patashu Jun 28 '13 at 03:39
-
1How they are written. – squiguy Jun 28 '13 at 03:40
-
1@Patashu: I don't believe there is any possible expression for `a` which would behave that way in Java. – Jun 28 '13 at 03:41
-
Was wonderful to see downvotes without understanding the context, even after the comments – Arun P Johny Jun 28 '13 at 03:42
-
NB: The question used to read `a=a+i`; I've converted the `i` to `b` for readability. – Jun 28 '13 at 03:45
-
When i was giving interview, that fellow said that there is performance point. a=a+i requires three fetches from memory and a+=i requires only two. how's that? – Sagar Jun 28 '13 at 03:46
-
There is a difference because the 1 is automatically assumed to be of the proper type to be added to a when += 1 is used. Saying a = a + 1 can cause a type casting error sometimes when += 1 would not. – Joseph Myers Jun 28 '13 at 03:47
1 Answers
0
The generated code will be the same for both. The only difference is to the readability of the code.

AMADANON Inc.
- 5,753
- 21
- 31
-
Just to point out this is not exactly the case, [here's one of the highest voted java questions](http://stackoverflow.com/questions/8710619/java-operator). – Zong Jun 28 '13 at 03:52