[Solved] Java preincrement and postincrement operators


Part one should print i = 2. This is because:

public class Sample 
{
    public static void main(String[] args) throws Exception 
    {
        //part 1
        int i=1;
        // i++ means i is returned (which is i = 1), then incremented, 
        //  therefore i = 1 because i is incremented to 2, but then reset 
        //  to 1 (i's initial value)
        i=i++;
        // i is incremented, then returned, therefore i = 2
        i=++i;
        // again, first i is returned, then incremented, therefore i = 2 
        //  (see first statement)
        i=i++;
        System.out.println(i);

        //part 2
        i=1;
        // first i is returned then incremented, so i = 2, a = 1
        int a=i++;
        // i is incremented then returned, so i = 3 and a = 3
        a=++i;
        // i is first returned, then incremented, so a = 3 and i = 4
        a=i++;
        System.out.println(a+"\n"+i);
    }
}

Perhaps the simplest way to understand this is to introduce some extra variables. Here is what you have:

// i = i++
temp = i;
i = i + 1;
i = temp; // so i equals the initial value of i (not the incremented value)

// i = ++i;
i = i + 1;
temp = i;
i = temp; // which is i + 1

// i = i++
temp = i;
i = i + 1;
i = temp; // so i equals the previous value of i (not i + 1)

Notice the difference in order of when the temp variable is set–either before or after the increment depending on whether or not it’s a post-increment (i++) or a pre-increment (++i).

1

solved Java preincrement and postincrement operators