-2

Could explain me why there is a difference when I want to sum some numbers?

int a = 4;
int b = 6;
int e = 10;

int wynik1 = a += b += e; //so it is 20. 
System.out.println(wynik1);
int wynik2 = a + b + e; 
System.out.println(wynik2); // so it is 46.... 

Should I just use always += instead of +? I'm confused because when I was learning, for example, loops, I was using for (int p = 20; p<40; p=p+ 5) and it was working fine.

Why is it 46?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • say a=1, b=1, a+=b. then the final value of a would be: a = 1 +1. a+=SOMETHING means add something to my current value of a, and assign it to a. –  Jan 20 '16 at 20:20
  • Another introduction to += is provided by the similar question ["What is the difference between a += b and a =+ b , also a++ and ++a?"](http://stackoverflow.com/questions/5098282/) – Andy Thomas Jan 20 '16 at 20:29
  • 2
    Is your question really just "What does += do?" – David Schwartz Jan 20 '16 at 21:33
  • the second line is `42`, but if you switched the order of the lines (so that the `+=` operator is **after** the `+` operator, you would have them both be `20`. – John S. Jan 21 '16 at 18:11

7 Answers7

5

In most cases (exception) a += b as equivalent of a = a + b

So

int wynik1 = a += b += e; //so it is 20. 

is same as

int wynik1 = (a = a + (b = b + e)); // so it is 20.

which means that

  • first b = b + e will be executed making b 6 + 10 = 16
  • then since b is 16 a will be assigned with result of 4 + 16 = 20
  • which finally will be assigned to wynik1.

So after that line (a = a + (b = b + e)) (or in your case a += b += e;) our variables will hold these values:

  • a = 20
  • b = 16
  • e = 10 (e didn't change since there was no e=.. in our code)

This should explain why

int wynik2 = a + b + e; //20 + 16 + 10

is 46.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

+= is way different from +.

a+b means add a and b and do something with the result.

a += b means add a and b and assign the result back to a.

In your example I don't think you want to have that kind of side effect in the first expression as you probably want to use the original values in the next expression.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
2

The += operator is distinct from the + operator in that it also assigns the result back to the variable.

a = a + 5;
a += 5;

Are equivalent.

In your example,

int wynik1 = a += b += e;

Not only is wynik1 equal to 20, but a is now also equal to 20 and b is now 16. This is why your second line returns 46:

   a +  b +  e
= 20 + 16 + 10
= 46
0

As for the line:

int wynik1 = a += b += e;

b will be added with 10, a with b, which is now 16, so the result is 20

The difference with a simple + is that the value of b is changed as well.

You could rewrite this to:

b = b + e; //results in b equals16
a = a + b; //results in a equals 20
Stefan
  • 17,448
  • 11
  • 60
  • 79
0

The operator += is a shortcut. Doing: a += 1; is equivalent to a = a + 1;

So, when you do: int wynik1 = a += b += e; is reality you are doing:

int wynik1 = (a = a + (b = b + e));
renanleandrof
  • 6,699
  • 9
  • 45
  • 67
0

+ just adds numeric values

+= adds the value on the right to the variable on the left part

Examples:

int a=3;
int b=4;
int c = a+b; // result c==7
c += 1; // result c==8

You can also use any other operator, like - * /

int d = 4;
d -= 3; // result d=1
int e=13;
int f *=3; // result f=39
trainrobbery
  • 500
  • 3
  • 14
0

First, note that

a += b;

is equivalent to

a = a + b;

The order of operations for assignment in Java is right to left. So,

a += b += e;

is

b = b + e; //16
a = a + b; //20
wynik1 = a; //20

Hence

wynik2 = a + b + e; //46
John Doe
  • 55
  • 1
  • 8