Table of content

What does ++ mean in Java?

The ++ is called the increment operator. It increases the value of the variable by 1. It can be used as a pre-increment or post-increment operator.

Pre-increment

The position of the ++ operator before a variable makes it a pre-increment operator. If i is a variable, ++i is called a pre-increment operator.

In this operation, the value of i is first incremented and then used in the operations involving i.

Let us take the variable i, the operation ++i is equal to i = i + 1. The value of i is first incremented and the value is obtained for use in other operations.

i = 42;
j = ++i; // j is set to 43 because the increment occurs before i is assigned to j
j = ++i  is equivalent to i = i + 1, then j = i
Pre-increment operators can be used to increment numeric data types.
For int data type

Pre-increment can be used to increment the value of integer data type by 1.

Example program illustrates the use of increment operator.

public static void main(String[] args) {
  int var1 = 5, var2;
  var2 = ++var1;
  // var2 is first assigned 6
  System.out.println(var1);
  // then var1 is also increased to 6
  System.out.println(var2);
}

The output is

6
6
For float data type

The pre-increment operator can be used to increment the value of a float variable by 1.

Example Program is given below

public static void main(String[] args) {
  float var1 = 1000.05 f, var2;
  var2 = ++var1;
  // var2 is first assigned 1001.05
  System.out.println(var1);
  // then var1 is also increased to 1001.05
  System.out.println(var2);
}

The output is

1001.05
1001.05
For double data type

A Pre-increment operator can be used to increase the value of a double data type by 1.

The example illustrates the use of the increment operator.

public static void main(String[] args) {
  double var1 = 50000, var2;
  var2 = ++var1;
  // var2 is first assigned 50001
  System.out.println(var1);
  // then var1 is also increased to 50001
  System.out.println(var2);
}

Output

50001.0
50001.0
For short data type

A Pre-increment operator can be used to increment a short variable.

Example

public static void main(String[] args) {
  short var1 = -1000, var2;
  var2 = ++var1;
  // var2 is first assigned -999 since -1000 + 1 = -999
  System.out.println(var1);
  // then var1 is also increased to -999
  System.out.println(var2);
}

Output is

-999
-999
For long data type

The pre-increment operator can also be used to increment the value of a long variable by 1.

The example below illustrates the use of pre-increment operation for a long data type.

public static void main(String[] args) {
  long var1 = 2540, var2;
  var2 = ++var1;
  // var2 is first assigned 2541
  System.out.println(var1);
  // then var1 is also increased to 2541
  System.out.println(var2);
}

Output

2541
2541
For byte data type

The byte data type can be incremented by using the pre-increment operator.

Example

public static void main(String[] args) {
   byte var1 = 120, var2;
   var2 = ++var1;
   // var2 is first assigned 121
   System.out.println(var1);
   // then var1 is also increased to 121
   System.out.println(var2);
 }

Output

121
121

Post increment

The position of the ++ operator after the variable makes it a post-increment operator. If i is variable, the i++ is a post-increment operator.

In this operation, the value of i is used in the operations and then incremented.

Let us take a variable i, the operation i++ is also equal to i = i + 1. The value of i is first used and then incremented for use of i in other operations.

i = 10;
j = i++; // j is set to 10 because the increment occurs after i is assigned to j
j = i++ is equivalent to i = j, then i = i + 1;
Post-increment operators can be used to increment numeric data types.
For int data type

Post-increment can be used to increment the value of integer data type by 1.

Example program

public static void main(String[] args) {
   int input1 = 340, input2;
   input2 = input1++;
   // input2 is first assigned 340
   System.out.println(input2);
   // then input1 is increased to 341
   System.out.println(input1);
 }

Output is

340
341
For float data type

The post increment operator can be used to increment the value of float data type variable by 1 in a program.

public static void main(String[] args) {
  float input1 = 2000.09 f, input2;
  input2 = input1++;
  // input2 is first assigned 2000.09
  System.out.println(input2);
  // then input1 is increased to 2001.09
  System.out.println(input1);
}

The output is

2000.09
2001.09
For double data type

The post increment can be used to increment the value of of a double data type variable by 1.

Example illustrates the use of post increment operator for double data type.

public static void main(String[] args) {
   double input1 = 45000.008, input2;
   input2 = input1++;
   // input2 is first assigned 45000.008
   System.out.println(input2);
   // then input1 is increased to 45001.008
   System.out.println(input1);
 }

The output is

45000.008
45001.008
For short data type

The post-increment operator can be used to increment the short data type.

Example program

public static void main(String[] args) {
   short input1 = -520, input2;
   input2 = input1++;
   // input2 is first assigned -520
   System.out.println(input2);
   // then input1 becomes -519 as -520 + 1 = -519
   System.out.println(input1);
 }

The output is

-520
-519
For long data type

The post increment operator can be used to increment the long data type variable by 1.

Example below illustrates the use of post increment operator to increment the long data type.

public static void main(String[] args) {
    long input1 = 12505, input2;
    input2 = input1++;
    // input2 is first assigned 12505
    System.out.println(input2);
    // then input1 is incremented to 12506
    System.out.println(input1);
  }

Output

12505
12506
For byte data type

The post-increment operator can be used to increment a byte data type variable by 1.

public static void main(String[] args) {
    byte input1 = 115, input2;
    input2 = input1++;
    // input2 is first assigned 115
    System.out.println(input2);
    // then input1 is incremented to 116
    System.out.println(input1);
  }

The output is

115
116
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions