7

So I was testing out operators, because im helping my friend with java and i stumbled across a weird order of programming. What is happening when I run the following code

public static void main(String[] args) {
    int B = 6;
    //First console print out
    System.out.println(B+=++B);
    System.out.println(B);
    B = 6;
    //Second Console print out
    System.out.println(B+=B++);
    System.out.println(B);
}

The output for the following code is

13
13

12   
12

What causes the second console B math output = 12 when it adds 6 to itself, followed by ++ (which is +1)

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Loligans
  • 497
  • 9
  • 24
  • "What causes the second B math input" This is really difficult to read. Can you reword this? – Daniel Kaplan Sep 09 '13 at 22:19
  • i added code commenting, is that easier to understand? – Loligans Sep 09 '13 at 22:21
  • 1
    In short: `++n` is pre-increment (increments value **before** it is used) and `n++` is post-increment (increments value **after** it is used) – Josh M Sep 09 '13 at 22:43
  • x=1, x++ will increase x to 2 but will return 1 (if you use it), in the other hand ++x will increase it to 2, but will return 2 instead of 1. Both have the same final state. It is the same as above but with apples. – porfiriopartida Sep 10 '13 at 00:40

8 Answers8

8

The difference here is in the increment operators.

In the case of B += ++B, B is incremented to 7 and added to its old self (6) to achieve 13.

In the case of B += B++, B is added to itself, giving 12, then the ++ is performed and the result stored in B, but the result of the calculation is then stored over the ++ in B. Thus giving 12 as the output.

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
5
ACTION       EFFECT   NOTES
----------   ------   -----
B = 6        B = 6
B += (++B)   B += 7   // B is incremented and the value is returned
B            B = 13

B = 6        B = 6
B += (B++)   B += 6   // The current value of B is the result of (B++) and B is
                      // incremented *after* the (B++) expression; however, the
                      // assignment to B (+=) happens after the (++) but it does
                      // not re-read B and thereby covers up the post increment.
B            B = 12
user2246674
  • 7,621
  • 25
  • 28
2

it adds 6 to itself, followed by ++

That's not quite what happens. It adds the result of B++ (i.e. the value that B++ evaluates to) to B. B++ evaluates to 6 because the postfix increment operators evaluates to the value of the operand before the increment. So it adds 6 to B.

You're thinking that the increment should happen after the assignment, but that's not how ++ works. The increment happens right away, but the expression evaluates to the value of B before the increment. So since you reassign B right after B++ is evaluated, the increment is undone by the assignment.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
2

When it comes to pre-increment and post-increment operators, always remember that they are applied immediately before/after the variable in question is read, and before the left hand side or assignment is processed.

E.g.

int a = 2;
a = a++ + a++ + a++;

a will be 9 since it ends up being a = 2 + 3 + 4;

Likewise

int a = 2;
a = a++;

a will still be 2, since the assignment happens after the increment, and the increment happens after the read.

int a = 2;
a = ++a;

a will be 3, since the assignment happens after the increment, but the increment happens before the read.

In your second example, the post-increment happens after the read and so the incremented B never has a chance to be read (not used again in the expression) before the assignment occurs (an assignment which overwrites B and any affect the post-increment would have had).

If you do:

int B = 6;
int C = B + B++;

Then you will end up with B equaling 7, while C will equal 12. Whereas B++ + B or B + ++B will both end up with C equaling 13 and B still equaling 7.

Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40
0

b+=x is the same as b = b + x.

You are doing b+=b++ so it can be written as b = b + b++ which for b=6 will be

b = 6 + 6;

because second parameter will be first evaluated to 6 (b++ will first return original value, then increment b) and since you assigning result (12 = 6 + 6) to b incrementation wont matter and b will become 12.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • So what your saying is the compiler grabs the b values before and therefore the ++ means nothing because b will be set to B + B – Loligans Sep 09 '13 at 22:29
  • The ++ is executed, but is done on the old value of B. The result of the calculation (12) is then stored over it in B. – Sinkingpoint Sep 09 '13 at 22:31
  • @Loligans Exactly. You can test it with `int x = 1; System.out.println(x++ +":"+ x);`, and `int x = 1; System.out.println(++x +":"+ x);`. You will see that `x++` will return current value and then inclement it, while `++x` will increment value first, and then return it. – Pshemo Sep 09 '13 at 22:34
  • @Loligans other neat example would be `b=b++`. This will not change `b` because it will 1) get value from b, 2) b++ will increment it, 3) `b=...` will again store value from step 1 to `b`. – Pshemo Sep 09 '13 at 22:38
  • *"`b+=x` is the same as `b = b + x`"* In most contexts, yes, but not always. – arshajii Sep 09 '13 at 22:50
  • @arshajii [true](http://stackoverflow.com/questions/8710619/java-operator) but lets not add details that are not required :) – Pshemo Sep 09 '13 at 22:53
  • Absolutely; I just mentioned it for the sake of completeness. – arshajii Sep 09 '13 at 22:55
0

rewriting and replacing: B = 6 + 6 after this, B++ is executed because B++ takes the current value from B then is incremented, so when B++ occurs, it doesn't matter, the B value will be 12

OscarG
  • 385
  • 5
  • 11
0

When you put the increment operator in front of the variable, it increments and then shows the variable. When you put the increment operator after the variable, it shows the variable, then adds 1 to it.

Stefan Carlson
  • 372
  • 3
  • 7
  • 21
0

++B means: increment the value, then return it. B++ means: return the value then increment it.

Arash Saidi
  • 2,228
  • 20
  • 36