Prefix and Postfix Increment and Decrement Operators in Java

Java Increment and Decrement Operators

Java provides two increment and decrement operators which are unary increment (++) and decrement (--) operators. Increment and decrement operators are used to increase or decrease the value of an operand by one, the operand must be a variable, an element of an array, or a field of an object.

The increment and decrement operators increases or decreases the value of an int variable by 1 or of a floating-point (float, double) value by 1.0. The unary increment and decrement operators can also be applied to char variables to step forward or backward one character position in the Unicode sorting sequence. These operators are known as unary operators because they are applied to a single variable.

The increment and decrement operators are used in prefix or postfix manner. If the operator is placed before the variable it's called prefix mode of increment and decrement.

During an assignment of one variable to other the prefix mode of increment and decrement first increments or decrements the variable's value then updated value of the variable is used in assignment. On the contrary, in postfix mode of increment and decrement first variable is used in assignment then the variable is incremented or decremented.

Note that prefix and postfix mode of operations make no difference if they are used in an independent statement, where just the value is incremented or decremented but no assignment is made.

Now, let us discuss increment and decrement operators in detail.

Increment Operator (++)

The ++ operator increments its single operand by one. The behavior of increment operator during an assignment operation depends on its position relative to the operand whether it is used in prefix or postfix mode. When used in prefix mode, it increments the operand and evaluates to the incremented value of that operand. When used in postfix mode, it increments its operand, but evaluates to the value of that operand before it was incremented.

Let's take an example to see the behavior of prefix and postfix form of Java's increment operator.

int x = 5, y;
 
// Demonstrating prefix increment
// first x will be incremented then
// updated value of x will be assigned to y
y = ++x;
System.out.println("y : " + y); //will print y : 6
System.out.println("x : " + x); //will print x : 6
 
// Demonstrating postfix increment
// first value of x will be assigned to y
// then x will be incremented
y = x++;
System.out.println("y : " + y); //will print y : 6
System.out.println("x : " + x); //will print x : 7
 
//If increment is made in an independent
//statement, prefix and postfix modes make no difference.
++x;
System.out.println("x : " + x); //will print x : 8
 
x++;
System.out.println("x : " + x); //will print x : 9

Decrement Operator (--)

The -- operator decrements its single operand by one. The behavior of decrement operator during an assignment operation depends on its position relative to the operand whether it is used in prefix or postfix mode. When used in prefix mode, it decrements the operand and evaluates to the decremented value of that operand. When used in postfix mode, it decrements its operand, but evaluates to the value of that operand before it was decremented.

Let's take an example to see the behavior of prefix and postfix form of Java's decrement operator.

int x = 5, y;
 
// Demonstrating prefix decrement
// first x will be decremented then
// updated value of x will be assigned to y
y = --x;
System.out.println("y : " + y); //will print y : 4
System.out.println("x : " + x); //will print x : 4
 
// Demonstrating postfix decrement
// first value of x will be assigned to y
// then x will be decremented
y = x--;
System.out.println("y : " + y); //will print y : 4
System.out.println("x : " + x); //will print x : 3
 
//If decrement is made in an independent
//statement, prefix and postfix modes make no difference.
--x;
System.out.println("x : " + x); //will print x : 2
 
x--;
System.out.println("x : " + x); //will print x : 1

Strange Behavior of Java Postfix Operators

Sometimes you may see the postfix form of increment or decrement operator behaving strangely. For an example, take look at the following piece of code:

int x = 1;
x = x++;
System.out.println("x : " + x); //will print x : 1

After reading the above piece of code carefully you may have guessed that x would have been 2 but you get 1. If you are a C or C++ programmer then you know what the postfix increment operator (++) does. This is of course not a bug in Java, and it has a legitimate reason.

Before going to the reason it is recommended that if you come across x = x++; type of code syntax, you should immediately replace it by x++. Now, let's investigate why does it behave strangely?

By definition postfix increment or decrement operator first returns the original value of the operand then increments the operand. In Java, postfix operator has higher precedence than assignment operator, so the x++ returns the original value of x, not the incremented one. Then meanwhile x gets incremented and becomes 2. But finally x is assigned the original value returned by x++ that was 1.

Last Word

In this tutorial we talked of Java's increment and decrement operators. Java's increment and decrement operators can be applied in prefix and postfix forms. Hope you have enjoyed reading this tutorial on various Java operators. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.