Read Java Interview Questions and Answers for Beginners and Experienced

Java Decrement operator Example

In this Java examples of Java tutorial, we will study about Java decrement operator example. We will study what is the use of Java pre decrement operator and java post decrement operator with example.

 


Java decrement operator

Java decrement operator is used to decrement the value of the variable.

 


Syntax of Java decrement operator

x= --x

• Here X is the integer variable whose value is decreased by 1.

 


Java decrement operator Example

Let’s understand Java decrement operator with example.

public class JavaExamples {

public static void main(String[] args) {
//Java decrement operator with example in Java
int num=20;
num = --num;
System.out.println("result is:"+num);

}

}

output

result is:19

Explanation
In above Java decrement operator program, num is integer variable. -- is decrement operator when we applied decrement operator on num variable, it increases the value with by default 1.

 


Types of decrement operator

There are two types of decrement operators:

Pre decrement Java operator

 

Post-decrement Java  operator

 


Pre decrement Java operator

Pre decrement java operator is that decrement operator where value is first decremented and then assign to the variable.

 


Pre-decrement Java operator example

Let’s understand pre-decrement java operator by taking an example.

public class JavaExamples {

public static void main(String[] args) {
//Java pre decrement operator with example in Java
int n1=0;
int n2=0;
n1=--n2;

System.out.println("n1 is:"+n1+"\n n2 is "+n2);

}

}


 

Output

n1 is:-1
n2 is -1


 

Explanation

In above pre decrement java operator, there are two variables n1 and n2. both are initialize with 0.

when decrement operator is assigned to n2 variable and assign that value to n1 at line

n1=--n2;

It will first decrement the value of n2 variable to -1, which will become 0-1=-1.

When we printed both the values of n1 and n2 it will come as -1 and -1. here -- n2 will be called as pre-decrement because decrement operator — is applied before the variable n2.

 


Post decrement Java operator

In Post decrement Java operator value is first assigned before its decrement.

 


Post decrement Java operator example

Let’s understand post-decrement java operator by taking an example.

public class JavaExamples {

public static void main(String[] args) {
//Java post decrement operator with example in Java
int n1=0;
int n2=0;
n1=n2--;

System.out.println("n1 is:"+n1+"\n n2 is "+n2);

}

}


 

Output

n1 is:0
n2 is -1


 

Explanation

In the above post decrement java operator example, we took same n1 and n2 two variable with 0 value. We apply post decrement operator on n2 variable and assign it to n1.

Now, notice the output:

n1 is 0 and n2 is -1.

how?

Value of the n2 is assigned to n1 before it got decrement. so initially n2 was 0, so 0 was assigned to n1, that’s why n1 is 0.

now, n2 is decremented, so in the next line when we printed the value of n2. it evaluates as -1.

 


Important things to note

There are few things to note or need to understand for Java decrement operator.

  • first thing is that when — operator is applied to any variable,by default it increases the value by 1.
  • second and import thing to note that we can apply — operator before or after the variable name like

x=x--  or  x=--x

x=x– is called post-decrement Java operator.

x=–x is called pre-decrement java operator.

  • Its not necessary to assign the value of decrement operator to any value. if you don’t assign then also it will work properly. see below
public class JavaExamples {

public static void main(String[] args) {
//Java decrement operator with example in Java
int n1=0;
n1--;

System.out.println("n1 is:"+n1);

}

}

Output

n1 is:-1 

Explanation

In above example, we did not assign the decremented value to any operator, yet the value of n1 is decreased.

 

So, In this Java operators with example, we saw how Java decrement operator works with example, also we saw what is pre decrement operator and post decrement operator in Java with example.