0

Can someone please explain why the result for z equals 12? I've read about pre- and post increment but this right here still doesn't make sense for me.

public static void main(String[] args) {
        int x = 1, y = 2, z = 3; 
        x = x + y;
        y = y + x;
        z = z + (x++) + (++y);  
        System.out.print("x = " + x + "\ny = " + y + "\nz = " + z);
     }
}

Like I see it:

x = x + y: x -> 1 (post increment by one) + y -> 3 (pre incremented by one), x = 4

y = y + x: y -> 4 (pre incremented by one again) + x -> 2 (post incremented by one), y = 6

z = z + (x++) + (++y): z -> 3 + x -> 3 (post incremented by one) + y -> 5 (pre incr. by one), z = 11

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
chr911
  • 11
  • 2
  • `x = x + y` has no post-increment or pre-increment in it, so why do you think those expressions involve any post-incrementing or pre-incrementing? – ajb Dec 17 '14 at 16:40
  • What Marcin Szymczak was saying is to use your IDE's debugger to step through the code. You can set a breakpoint and examine the variables as they change to see how different operations are affecting the results. – Paul Richter Dec 17 '14 at 16:41
  • 1
    If you're not using an IDE and trying to set it up would be too difficult, you can always rely on the `println` debugger :) :) :) – ajb Dec 17 '14 at 16:43
  • @ajb it was because when I ran the code, it gave me x = 4, y = 6. so I just thought that that was the way to do it. It's clear for me now that I really misunderstood the whole thing. – chr911 Dec 17 '14 at 17:25

3 Answers3

3

See notes in code below.

The main problem with your own annotations in the question is that you're seeing pre-increment and post-increment operators where there aren't any. In x = x + y, for example, this is a simple assignment with no increments anywhere.

public static void main(String[] args) {
        int x = 1, y = 2, z = 3;
            // so now (x,y,z) == (1,2,3)
        x = x + y;
            // so now (x,y,z) == (3,2,3)
        y = y + x;
            // so now (x,y,z) == (3,5,3)
        z = z + (x++) + (++y);
            // here, x++ would return 3 (unincremented)
            //       ++y would return 6 (incremented)
            // so now z = 3 + 3 (unincremented x) + 6 (incremented y) == 12
            // and in the process both x and y have been incremented
            // so we have (x,y,z) == (4,6,12)
        System.out.print("x = " + x + "\ny = " + y + "\nz = " + z);
     }
}
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
2
y = x++; // This assignes x 's value to y then increment x by 1
y = ++x; // This increments x by one then assigns new x's value to y.

On your code

    int x = 1, y = 2, z = 3; 
    x = x + y;
    // x = 3
    y = y + x;
    // y = 5
    z = z + (x++) + (++y); 
    // z = 3 + 3 + 6 --> 12

This z = z + (x++) + (++y); code is equal to combination of below 3 lines of code:

y = y + 1; 
z = z + x + y;
x = x + 1;
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
  • But when I run the code, x = 4, y = 6 and then as you say z = 12 – chr911 Dec 17 '14 at 16:53
  • @chr911 `x` and `y` are 3 and 5 **before** the last statement (the one that assigns `z`). The last statement sets `z` to 12, and it also causes `x` and `y` to increase by 1, so that they're 4 and 6 when you do the `print`. – ajb Dec 17 '14 at 17:01
  • Yes x is 4 at the end, As it (x++) in the last operation. X's value of 3 is used for the operation, then x is incremented by 1. – Salih Erikci Dec 17 '14 at 17:01
  • @chr911 see the last part of my answer. – Salih Erikci Dec 17 '14 at 17:09
  • 1
    @SalihErikci Thanks for your help! Appreciate it. – chr911 Dec 17 '14 at 17:30
1

Let's make a simple analysis on the line z = z + (x++) + (++y); (no real code):

z = (previous value of z: 3) + 
    (the existing value of x (increment will take place after the plus expression evaluation): 3 (1 + 2 from previous assign statement)) +
    (the value of y after being incremented by 1: 6 (2 + 3 from previous assign statement + 1 from the increment)). =>
z = 3 + 3 + 6 =>
z = 12 //Assign 12 value to z
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54