# Java Program on Increment Operator

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

## Java Program on Increment Operator

Increment Operator:

Increment operator is one of unary operator which is used for increment by 1. Means it increases the operand value by 1. Increment operator is denoted by ++ (double plus) symbol.

There are two types of increment operator.

Some points to remember while using increment operator.

1. Increment 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 increment operator. (Eg. ++(++var))

Let’s see one by one.

### Pre Increment Operator:

When ++ symbol is used before the operand then that is called as pre increment operator.

Syntax: ++operand

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

Example:

int a = 1;
++a;  //Now value of a=2
int b = ++a;  //Now value of b=3 and a=3 (First Incremented 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 increment of args
//pre increment: update value then assign
//so b value will be incremented value
int b = ++a;
//Printing value of b
System.out.println("Value of b: "+b);
System.out.println("Value of a: "+a);
}
}
Output:

Value of a: 5
Value of b: 6
Value of a: 6

### Post Increment Operator:

When ++ symbol is used after the operand then that is called as post increment operator.

Syntax: operand++

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

Example:

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

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 increment of args
//post increment: 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 incremented 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: 6

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.