Decrement and Increment Operators in JAVA

Anna Scott
4 min readOct 7, 2021

Java has a number of operators for working with variables.

In this post I focus on increment “++” and decrement “ — “ operators.

The job of increment operator to increase the value by one, and it would only make sense that decrement operator would decrease the value by one. Both (increment and decrement) operators can precede of follow the operand (value in question).

When placed before the operand/variable (++variable, — variable), the result of their application goes immediately into action. If operand/variable is followed by “++” or “ — “ the effect of their action does not take place until the next statement. Take a look at the following program:

public class IncrementDecrementOperators {
public static void main(String... args) {
int x = 0;

System.out.println("Workings of ++ ");
System.out.println("We start with x=" + x);
//when ++ placed before increases the value bu 1 right away System.out.println("++x=" + ++x); //the previous value retained until the execution of current
//statement, inceases by 1 for the next
System.out.println("x++=" + x++);
System.out.println("next statement x=" + x + "\n");

x = 1;
System.out.println("We start with x=" + x );
System.out.println("Workings of -- ");
//when -- placed before decreses the value bu 1 right away System.out.println("--x=" + --x); //the previous value retained until the execution of current
//statement, decreases by 1 for the next
System.out.println("x--=" + x++);
System.out.println("next statement x=" + x);
}
}

The output:

Workings of ++
We start with x=0
++x=1
x++=1
next statement x=2

We start with x=1
Workings of —
— x=0
x — =0
next statement x=1

When we have somewhat complex expression with multiple increment and decrements, we need to make sure that we know our operator precedence:

Take a a look at the following program:

public class ComplexExpression {
public static void main(String... args){

int x = 1;
int y = ++x + ++x * 5 / x-- + --x + ++x;

System.out.println("x=" + x);
System.out.println("y=" + y);

}
}

Its output:

x=2
y=10

Lets look at how JAVA does calculations step by step:

  1. y = 2+ ++x*5/x — + — x + ++x;
  2. y = 2+ 3*5/x — + — x + ++x;
  3. y = 2+ 3*5/3+ — x + ++x;
  4. y= 2+ 3*5/3 + 1+ ++x;
  5. y = 2 + 3*5/3 + 1+ 2;
  6. y = 10

Our increment and decrement operators have the highest precedence, so we have to calculate their result first. We read our expression from left to right: right after “y=” the first “++x” is going to be 2. After the first “+”, the second “++x” is going be 3. The “x — “ is going to be using 3, though it will arrive to “— x” as 2, and “ — x” becomes 1. The last “++x” becomes 2. 2 is our final value for x. After doing our math we see that our y equals 10.

Keep in mind that increment and decrement operators meant to be used alone. In the following example, we run into infinite loop, when we try postfix increment operator with assignment operator:

public class InfiniteLoop {
public static void main(String... input) {
for (int i = 0; i < 4; i = i++ ) {
System.out.println("Hi");
}
}
}

Here in update statement: “i = i++” the local variable i really is not being updated, since increment goes into effect after previous value was was assigned to variable i.

Happy coding my friends!

You can find all code here:

https://github.com/forfireonly/MediumArticlesJava/tree/master/src/Operators

--

--