Q:

Confusion about result of pre and post decrements

I recently started learning java in my computer science class and i came accross something interesting. 
Basically i have my variables set as the following 
int x = 5;
I created an expression x = (x--) - (--x)
when i print the variable x it always results as 2, no matter what the variable x is.  
The exact code is:
import acm.program.*; 

public class MathOperations extends ConsoleProgram {

    private static final long serialVersionUID = 1L;

    public void run(){

        setFont("arial-bold-24");

        int x = -5, y = -3;
        double z = 2.2;

        x = (x--) - (--x);

        println ("x= " + x);
    }

}

Why is the result always 2? 

A:

The result is always 2, for any int you can use.   The reason:
(x--) is evaluated first, because operands are evaluated left-to-right.   This yields the original value, x, but afterwards, x is original x - 1.   Then (--x) is evaluated.   It returns x - 1, which was already decremented once, so the result is original x - 2. 
From there, it's simple math, x - (x - 2) is 2. 
But what about possible underflow in the decrement? 
int x = Integer. MIN_VALUE + 1;
x = x-- - --x;
System.out.println(x);

The result is still 2.   How?   Integer. MIN_VALUE is:
10000000 00000000 00000000 00000000

Decrementing it twice will yield Integer. MAX_VALUE - 1. 
01111111 11111111 11111111 11111110

Subtraction in 2's complement notation yields
00000000 00000000 00000000 00000010

or 2, even if underflow takes place. 
That is true for any integral type: byte, short, int, and long. 
Interestingly, it will also be true for most common floating-point values, such as 2.2. 
However, if the precision of the floating-point type is more than 1, then the result of the decrement operation will not change the value.   In this case, the result will be 0. 
double x = (double) (1L << 54);
x = x-- - --x;
System.out.println(x);

Result:
0.0

