class TestPrefixAndPostfix {

    public static void main(String[] args){
        int x = 0;
        int y = 0;
        
        // a compile error occurs if used directly with a value
        // ++5;
        
        // same result as x = x + 1
        ++x;
        System.out.println("x=0; ++x; \t\t\t -> " + x);
        
        x = 0;
        x++;
        System.out.println("x=0; x++; \t\t\t -> " + x);
        
        // same result as x = x -1
        x = 0;
        --x;
        System.out.println("x=0; --x; \t\t\t -> " + x);      
        
        x = 0;
        x--;
        System.out.println("x=0; x--; \t\t\t -> " + x);  
        
        // Prefix operators used in expressions
        x = 0;
        y = 0;
        y = ++x;
        System.out.println("x=0; y=0; y = ++x; \t\t -> " + 
                           "y = " + y + " x = " + x );      
        
        x = 0;
        y = 0;
        y = x++;
        System.out.println("x=0; y=0; y = x++; \t\t -> " + 
                           "y = " + y + " x = " + x);  
                           
        // unexpected results
        x = 0;
        x = ++x;
        System.out.println("x=0; x = ++x; \t\t\t -> " +
                            x + "[Value of x changes]");                             
        
        x = 0;
        x = x++;
        System.out.println("x=0; x = x++; \t\t\t -> " +
                            x + "[Value of x does not change]");  
                            
        // test on char types
        char c = 'a';
        c++;
        System.out.println("char c = 'a'; c++; \t\t -> " + c);
        
        --c;
        System.out.println("char c = 'b'; --c; \t\t -> " + c);     
        
        // test on bytes
        byte b = 2;
        byte b1;
        System.out.println("byte b = 2; ++b; \t\t -> " + ++b);   
        
        b1 = ++b;
        System.out.println("byte b1 = 3; b1 = ++b; \t\t -> " + b1);   
        
        b = 127;
        b1 = ++b;
        System.out.println("byte b1 = 127; b1 = ++b; \t -> " + b1);  
        
        b = -128;
        b1 = --b; 
        System.out.println("byte b1 = -128; b1 = --b; \t -> " + b1);                      
    }
}
