DECREMENT OPERATOR


Decrement Operator: 
  • We will used decrement operator when we want to decrement the value by one.
  • There are two types of decrement operators we have.
  1. Predecrement operator:
   Rules: 
  • Decrement the value by one.
  • Update the decremented value in the same variable.
  • Any operation is going on we will used the decremented value.

    
  1. class Predecrement
  2. {  
  3. public static void main(String [] args)
  4. {  
  5. int c23
  6. int b= --c;
  7. int a--b;
  8. System.out.println(a);
  9. System.out.println(b);
  10. System.out.println(c);
  11. }

Output:

21                                                          
21                                                          
22                                                          


    2.Postdecrement operator: 
            

 Rules: 
  • Decrement the value by one.
  • Update the decremented value in the same variable.
  • Any operation is going on we will used the current value but not the decremented value.
  1. class Postpredecrement
  2. {  
  3. public static void main(String [] args)
  4. {  
  5. int c23
  6. int b= c--;
  7. int a--b;
  8. System.out.println(a);
  9. System.out.println(b);
  10. System.out.println(c);
  11. }

Output:

22                                                          
22                                                          
22