Java Unary Operator Increment Decrement Example


Program
public class UnaryOperator{ public static void main(String args[]){ int x=1; System.out.println(x++);//1 //(p1-Divide x++, into x and ++, //x will print first 1, then after printing it will get incremented by 1 making it 2) System.out.println(++x);//3 //(p2-Divdie ++x, into ++ and x, //++ will increment step-p1 value first making it 3, then x will print the value 3) System.out.println(x--);//3 //(p3-Divide x--, into x and --, x will print first the previous step-p2 value, //then after printing it will get decremented by 1 making it 2) System.out.println(--x);//1 //(p4-Divide --x, into -- and x, -- will first decrement step-p3 value making it 1, //then after it will print x vlaue i.e 1) }}
Input
Output