23

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
  • 115,165
  • 71
  • 313
  • 417

9 Answers9

38

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,717
  • 1
  • 21
  • 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
18

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
  • 114,404
  • 31
  • 268
  • 329
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
  • 178,883
  • 35
  • 278
  • 309
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,055
  • 6
  • 50
  • 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
  • 334,321
  • 69
  • 703
  • 674
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
  • 60,705
  • 7
  • 138
  • 176
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
  • 22,354
  • 11
  • 51
  • 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
  • 43,673
  • 4
  • 57
  • 93
pkthapa
  • 1,029
  • 1
  • 17
  • 27
-1

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

Bartzilla
  • 2,768
  • 4
  • 28
  • 37