21

As I mentioned in the title,

What is the difference between a += b and a =+ b , also a++ and ++a ? I'm little confused

Eng.Fouad
  • 111,301
  • 67
  • 311
  • 403

9 Answers9

35

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
Alex Deem
  • 4,627
  • 1
  • 20
  • 24
  • 1
    +1. Re: "`a += b` is equivalent to `a = a + b`": A small pedantic nit: if the evaluation of `a` involves side-effects, then those happen only once. For example, in `foo().x += y`, the `foo` method is called only once, whereas in `foo().x = foo().x + y`, it's called twice (and it could even return a different instance each time, in which case the `x` that's being assigned to is different from the `x` that's being read from). – ruakh Nov 06 '20 at 01:27
17

a += b is equivalent to a = a + b

a = +b is equivalent to a = b

a++ is postfix increment and ++a is prefix increment. They do not differ when used in a standalone statement, however their evaluation result differs: a++ returns the value of a before incrementing, while ++a after. I.e.

int a = 1;
int b = a++; // result: b == 1, a == 2
int c = ++a; // result: c == 3, a == 3
Péter Török
  • 112,083
  • 30
  • 265
  • 327
6

Others have covered the answers to most of your questions. However, they are missing a bit about your second example.

a = +b assigns the value of +b to a. The "unary plus" is a no-operation for numeric types, but a compile-time error on other types of objects (for example, you can't use it with a string). It is provided mainly so you can write numbers with a leading + sign when you want to. This is never necessary, but it can improve readability in some circumstances.

kindall
  • 168,929
  • 32
  • 262
  • 294
4

a+=b ========> a=a+b

a=+b ========> a=b

++a will increment the variable and return the incremented value.

a++ will increment the variable but return the value before it was incremented.

KingofBliss
  • 15,007
  • 6
  • 48
  • 72
3

Java operators

a += b;  // a = a + b
a = +b;  // a = b
a++;     // a = a + 1 (returning a if used inside some expression)
++a;     // a = a + 1 (returning a + 1 if used inside some expression)
Tomasz Nurkiewicz
  • 324,247
  • 67
  • 682
  • 662
2
a += b <=> a = a + b
a =+ b <=> a = b
a++ // post increment, means the value gets used, and after that, a is incremented by one
++a //pre increment, a is incremented by one before the value is used
Femaref
  • 59,667
  • 7
  • 131
  • 173
2

a++ first reads the value of a and then increments its value. ++a first increments the value and then reads it. You can see easily the difference printing them.

int a = 4;
System.out.println(a++); // prints 4, after printing, a == 5
System.out.println(++a); // first increments a, then reads its value (6), and that's what got printed.

for a += b and a = +b, @Péter Török has answered clearly before.

Carlos Campderrós
  • 21,266
  • 11
  • 50
  • 57
1
  • a += b; is equivalent to a = a + b;.

  • a =+ b; is equivalent to a = +b;. This means +b (positive) is assigned to variable a.

  • a++ is post increment of variable a, meaning the value of the variable is used before incrementing by 1.

  • ++a is pre-increment of variable a, meaning the value of the variable is incremented by 1 and used after increment.

Andrey Tyukin
  • 41,173
  • 4
  • 44
  • 82
pkthapa
  • 989
  • 1
  • 14
  • 24
-1

You can find the difference here There are examples for all the cases you mention!

Bartzilla
  • 2,748
  • 3
  • 25
  • 37