Java Operators – Difference Between a += b and a =+ b, a++ and ++a
java
As I mentioned in the title,
What is the difference between a += b and a =+ b , also a++ and ++a ? I'm little confused
Best Answer
a += b is equivalent to a = a + b
a = +b is equivalent to a = b
a++ and ++a both increment a by 1.
The difference is that a++ returns the value of a before the increment whereas ++a returns the value after the increment.
That is:
a = 10;
b = ++a; //a = 11, b = 11
a = 10;
b = a++; //a = 11, b = 10
Best Answer
a += b
is equivalent toa = a + b
a = +b
is equivalent toa = b
a++
and++a
both incrementa
by 1. The difference is thata++
returns the value ofa
before the increment whereas++a
returns the value after the increment.That is: