Java decrement operator – Java Program on Decrement Operator

Java decrement operator: In the previous article, we have discussed about Java Program on Increment Operator

In this article we will see the use of decrement operator in Java programming language.

Java Program on Decrement Operator

Decrement Operator:

Decrement operator is one of unary operator which is used for decrement by 1. Means it decreases the operand value by 1. Decrement operator is denoted by — (double minus) symbol.

There are two types of decrement operator.

Some points to remember while using decrement operator.

  1. Decrement operator can be used with variable not with constant as constant value can not be modified.
  2. The operand should not be an expression as it can not be updated.
  3. It can not be used over final variables.
  4. It can not be operated on Boolean value.
  5. Nesting can not be applied on decrement operator. (Eg. –(–var))

Let’s see one by one.

Pre Decrement Operator:

When — symbol is used before the operand then that is called as pre decrement operator.

Syntax: --operand

In case of pre decrement first the value is decremented and then it is assigned. In more simple the operand value is decremented by 1 and then it returns that value. Means we first decrement the value then we use this decremented value in the expression.

Example:

int a = 3; 
--a;  //Now value of a=2
int b = --a;  //Now value of b=1 and a=1 (First Decremented the value then assigned)

Let’s see one program to understand it more clearly.

public class Main 
{
    // main method
    public static void main(String[] args)
    {
        //declaring value of a
        int a = 5;
        //Printing value of a
        System.out.println("Value of a: "+a);
        //performing pre decrement of args
        //pre decrement: update value then assign
        //so b value will be decremented value 
        int b = --a;
        //Printing value of b
        System.out.println("Value of b: "+b);
        //now decremented value of 'a' will be printed
        System.out.println("Value of a: "+a);
    }
}
Output:

Value of a: 5
Value of b: 4
Value of a: 4

Post Decrement Operator:

When — symbol is used after the operand then that is called as post decrement operator.

Syntax: operand--

In case of post decrement first the value is assigned and then it is decremented. In more simple the operand value is returned first then it is decremented by 1. Means we first use the value in the expression then we decrement the value.

Example:

int a = 3; 
a--; //Now value of a=2
int b = a--;  //Now value of b=2 and a=1 (First assigned the value then decremented)

Let’s see one program to understand it more clearly.

public class Main 
{
    // main method
    public static void main(String[] args)
    {
        //declaring value of a
        int a = 5;
        //Printing value of a
        System.out.println("Value of a: "+a);
        //performing post decrement of args
        //post decrement: assign value then update
        //so b value will be original value only
        int b = a--;
        //Printing value of b
        System.out.println("Value of b: "+b);
        //now decremented value of 'a' will be printed
        System.out.println("Value of a: "+a);
    }
}
Output:

Value of a: 5
Value of b: 5
Value of a: 4

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: