Read Java Interview Questions and Answers for Beginners and Experienced

Java Increment operator Example

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

 


Java Increment operator

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

Syntax of Java increment operator

 x= ++x

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

 


Java Increment operator Example

Let’s understand Java Increment operator with example.

public class JavaExamples {

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

}

}

output

result is:21

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

Types of increment operator

There are two types of increment operators:

Pre Increment Java operator

 

Post-Increment Java  operator

 


Pre Increment Java operator

Pre increment java operator is that increment operator where value is first incremented and then assign to the variable.

 


Pre-Increment Java operator example

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

public class JavaExamples {

public static void main(String[] args) {
//Java pre increment 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 increment java operator, there are two variables n1 and n2. both are initialize with 0.

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

n1=++n2;

It will first increment 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-increment because increment operator ++ is applied before the variable n2.

 


Post Increment Java operator

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

 


Post Increment Java operator example

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

public class JavaExamples {

public static void main(String[] args) {
//Java post increment 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 increment java operator example, we took same n1 and n2 two variable with 0 value. We apply post increment 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 increment. so initially n2 was 0, so 0 was assigned to n1, that’s why n1 is 0.

now, n2 is incremented, 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 increment 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-increment Java operator.

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

  • Its not necessary to assign the value of increment 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 increment 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 incremented value to any operator, yet the value of n1 is increased.

 

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