Turn Desktop View Off
by Dinesh Thakur

Java has two very useful operators. They are increment (++) and decrement (- -) operators. The increment operator (++) add 1 to the operator value contained in the variable. The decrement operator (- -) subtract from the value contained in the variable.

There are two ways of representing increment and decrement operators.

a) as a prefix i.e. operator precedes the operand. Eg. ++ i; - -j ;

b) as a postfix i.e. operator follows the operand. Eg. i- -; j ++;

In case of prefix increment operator (Eg. ++i), first the value of the operand will be incremented then incremented value will be used in the expression in which it appears.

In case of postfix increment operator (Eg. I++), first the current value of the operand is used in the expression in which it appears and then its value is incremented by 1.

Java Example to implement Increment Operator.

class IncrementOperator
{
         public static void main(String args[])
       {
               int X=14, Y=17;
               System.out.println("The Value X is : " +X);
               System.out.println("The Value Y is : " +Y);
               System.out.println("The Value ++X is : " +(++X));
               System.out.println("The Value Y++ is : " +(Y++));
               System.out.println("The Value X is : " +X);
               System.out.println("The Value of  Y is : " +Y);
       }
}

Java Example to implement Increment Operator

Java Example to implement Decrement Operator

class DecrementOperator
{
         public static void main(String args[])
       {
               int X=10, Y=12;
               System.out.println("The Value X is : " +X);
               System.out.println("The Value Y is : " +Y);
               System.out.println("The Value --X is : " +(--X));
               System.out.println("The Value Y-- is : " +(Y--));
               System.out.println("The Value X is : " +X);
               System.out.println("The Value of  Y is : " +Y);
       }
}

Java Example to implement Decrement Operator

Java Example to perform increment and decrement operations using Scanner Class.

import java.util.*;
class IncrementDecrementUsingScanner
{
                    public static void main(String args[])
           {
                      int a;
                      Scanner scan=new Scanner(System.in);
                      System.out.print("Enter the Value of a : ");
                      a=scan.nextInt();
                      System.out.println("Before Increment A : "+a);
                      System.out.println("After Increment A : "+(++a));
                      a++;
                      System.out.println("After Two Time Increment A : " +a);
                      System.out.println("Before Decrement A : "+a);
                      --a;
                      a--;
                      System.out.println("After Decrement Two Time A : "+a);
           }
}

Java Example to perform increment and decrement operations using Scanner Class