1
int m = 0;

m += m++;

System.out.println(m);

prints to 0 but i thought m will be post incremented and finally be 1. Would someone please explain.

Note: i know how post increment works (atleast i think i do :P). But what i am trying to figure out is that when i say m + = m++ lets assume it means m = m + m++ which will evaluate m++ after sending the value of m, 0 in this case, and evaluate to m = 0 + 0 then increment the value of m because of the post increment. now if post increment has occured, why is m not 1

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43

8 Answers8

4

m++ returns m then increments it.

Change it to ++m and you'll see 1.

For clarity, this question explains it too: What is x after "x = x++"?

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
4

Consider the following rules:

  • a += b is equivalent to a = a + b. Therefore m += m++ is equivalent to m = m + m++.
    You can see that the first occurence of m at the right sight is evaluated before the increment and produces 0

  • m++ increments m and returns original value of m before the increment.
    So, in your case m++ sets m to 1 and returns 0

  • Assignment happens after evaluation of the right side, whereas post-increment happens during evaluation of the right side.

Now you can see that your expression is evaluated as follows:

  • Evaluation of the right side produces 0 and sets m to 1

  • Assignment sets m to value of the right side (0)

axtavt
  • 239,438
  • 41
  • 511
  • 482
2

The sequence of events is:

  1. The RHS is evaluated (0)
  2. The post increment is done (m++).
  3. The evaluated result is assigned (m=0 again).

i.e. this is equivalent to:

tmp = m;
m++;
m = tmp;

If you did m = ++m, the sequence would be:

  1. The pre increment is done (++m).
  2. The RHS is evaluated (1)
  3. The evaluated result is assigned (m=1).
SteveP
  • 18,840
  • 9
  • 47
  • 60
2

ANSWER: An assignment evaluates side effects before performing the assignment.

m += m++;

is equivalent to

temp = m + m++;    ; m is now 1, but temp is 0
m = temp;          ; m is now 0 again
Kevin A. Naudé
  • 3,992
  • 19
  • 20
1
int m = 0

At this point m is 0

m += m++;

Expands to m=m+(m++)

m returns 0

m++ returns the value of m which is 0 and then increments it to 1

transforming m+(m++) into 0+0 (m is 1 a this point)

This is then assigned to m resulting in the final value of m.

Tip: avoid post-increment when you're touching the value otherwise

edofic
  • 727
  • 8
  • 15
1

In your code

m+ =m++; Results -- >> M value + (post incremented m value(m++))

Initially

m value= 0

post incremented(m++) m value = 0 (pre increment(++m) = 1)

kark
  • 4,763
  • 6
  • 30
  • 44
0

Well m++ return the value of m before to increment it, contrary to ++m that increment then return, like:

 int m++(int m){
     int tmp = m;
     m = m+1;
     return tmp; 
}


 int ++m(int m){
     m = m+1;
     return m; 
}
aveuiller
  • 1,511
  • 1
  • 10
  • 25
0

f is your code. g is an expanded version showing why they both print 0.

class Test {
    private static void f() {
        int x = 0;
        x += x++;
        System.out.println(x);
    }

    private static void g() {
        int x = 0;
        int preInc = x; // preInc = 0
        x += 1;         // x = 1
        x = preInc;     // x = 0
        System.out.println(x);
    }

    public static void main(String[] args) {
        f();
        g();
    }
}
Peter Sutton
  • 1,145
  • 7
  • 20