Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

How to Use the Java Decrement Operator

Understand how Java's decrement operator (`--`) works in prefix and postfix form, including evaluation order, common mistakes, and practical code examples.

~6 min readJava
#Java operators#unary operators#loop control#Java syntax
An abstract illustration of a Java decrement operator showing a minus arrow with a variable symbol.

The decrement operator -- subtracts one from a variable and stores the result back into that variable. It is a unary operator with two forms: prefix (--x) and postfix (x--). Both forms reduce the value by one, but they produce different values when used as part of a larger expression. Knowing which value each form returns is essential for writing correct loop conditions, index arithmetic, and any expression that relies on the operator's side effect.

In Java, the operand of -- must be a variable that can be assigned to; literals and method call results are not allowed. For example, 5-- and getCount()-- are invalid. The operator can be applied to integer types (int, long, short, byte), floating-point types (float, double), and char, but not to final variables.

Prefix and Postfix Semantics

The prefix form --x decrements the variable first and then returns the new value. The postfix form x-- returns the original value of x and then decrements the variable. This distinction matters only when the result of the decrement expression is used in a larger expression.

Consider this code:

java
int x = 5; int y = --x; // y is 4, x is 4

In contrast:

java
int x = 5; int y = x--; // y is 5, x is 4

In the first example, --x evaluates to the new value of x, which is 4, and that value is assigned to y. In the second example, x-- yields the old value, 5, before the decrement happens, so y gets 5 while x becomes 4. This behavior is consistent with the operator's definition: postfix operators return the original value, prefix operators return the updated value.

Behavior in Loop Conditions and Index Arithmetic

The most common use of the decrement operator is in for loops that iterate backward. For example, traversing an array from the last element to the first often uses a loop like this:

java
int[] numbers = {10, 20, 30, 40}; for (int i = numbers.length - 1; i >= 0; i--) { System.out.println(numbers[i]); }

Here, i-- is used in the update expression, and its return value is ignored. Only the side effect (decrementing i) matters. The distinction between prefix and postfix is irrelevant in this context because the expression's result is not used. However, when the operator appears inside a larger expression, the choice changes the result.

A more subtle pattern appears in algorithms that use an index and need to consume the current value while also advancing the index. For example, when popping items from a stack implemented with an array, you might write:

java
int[] stack = new int[100]; int top = 10; // number of elements int value = stack[--top]; // read the last element, then reduce top

This uses the prefix decrement to first reduce top and then use the new value as the index. The postfix version stack[top--] would read the element at the old top index, which would be out of bounds if top was initialized to stack.length. Understanding which form to use prevents off-by-one errors in such data structure operations.

Evaluation Order and Side Effects

The decrement operator is a side-effecting operation: it modifies the variable. Java specifies that operands are evaluated left to right, and whether the decrement happens before or after the original value is consumed depends on whether the operator is prefix or postfix. This becomes critical when the same variable appears more than once in one expression.

For example, consider:

java
int x = 5; int y = x-- + x; // y is 9? 10?

Java evaluates the left operand x-- first, which returns 5 and changes x to 4. Then it evaluates the right operand x, which is now 4. The sum is 9. This behavior is defined by the Java Language Specification, and no undefined behavior exists as in C or C++. However, relying on such ordering is often considered poor style because it makes code hard to read. A clearer approach is to separate the decrement from the arithmetic:

java
int y = x + (--x); // not recommended either

In this case, the prefix decrement applies to the second x after the first x is read, so if x was 5, the result is 5 + 4 = 9. But mixing such operations leads to confusion. For maintainable code, avoid using the decrement operator inline in expressions that also use the same variable elsewhere unless the sequence is obvious.

Common Pitfalls and Misunderstandings

A frequent mistake is confusing the decrement operator with the subtraction assignment operator -= 1. Both subtract one, but -= 1 always evaluates to the new value, regardless of position. For example, int y = (x -= 1); is equivalent to int y = --x; but not to x--. The -= operator does not have a postfix variant.

Another pitfall arises in method arguments. Consider:

java
int i = 0; foo(i--); // passes 0, then i becomes -1

If the intention was to pass the decremented value, --i must be used instead. This mistake is particularly common when implementing recursive algorithms that decrement a counter and need to pass the new value to a recursive call.

Also, using the decrement operator on a final variable causes a compile-time error. For instance, final int COUNT = 5; COUNT--; will not compile because COUNT cannot be modified. This is an obvious restriction but still trips up developers who attempt to use a constant as a loop counter.

Performance Notes

Treat the choice between --i and i-- as a semantic choice, not a performance optimization. When the result is unused, both forms have the same observable effect on the variable. If a shared field is decremented from multiple threads, protecting that mutation is more important than the operator itself; AtomicInteger and related classes provide atomic decrement methods.

Compatibility and Limitations Across Java Versions

The decrement operator is part of Java's core syntax and its semantics have remained stable. Java specifies evaluation order, so an expression such as x-- + x-- is defined: the left operand is evaluated first, returning the original x, then the right operand is evaluated, returning the decremented value, and the side effects are applied in that order. This deterministic behavior is useful when reading older Java code.

The operator does not allow literals as operands, so --5 is invalid. It also cannot be applied to expressions like x + 1; you cannot write (x + 1)--. This restriction is consistent with other programming languages that require an L-value operand.

When to Use the Prefix Form Instead

In practice, the prefix form --x is preferable when the resulting value is needed immediately, because it communicates the intent more directly. For instance, in a stack pop operation, value = stack[--top] clearly shows that you want the element at the new index. The postfix form is appropriate when the loop or logic depends on using the old value before decrementing, such as in a while (n-- > 0) pattern to execute a block exactly n times.

A common idiom for counting down with the postfix operator is:

java
int n = 3; while (n-- > 0) { System.out.println(n); // prints 2, 1, 0 }

In the loop condition, n-- returns the current value of n before decrementing it. The first test sees 3, so the body runs and prints 2, the value after that decrement. The last test sees 0, so the loop ends; n is -1 by the time the condition is false. This idiom works, but it can be confusing because the value that controls the loop is decremented in the condition, not at the end of the body. A for loop with an explicit update expression is often clearer.

The choice between prefix and postfix should be driven by the expression's semantics, not by stylistic preference. When the result is unused, both forms are equivalent and the choice has no effect on the execution. When the result is used, select the form that preserves the intended value. Reading code that unexpectedly uses postfix decrement in a context where the old value is not needed can mislead the reader; using prefix in such cases avoids unnecessary mental parsing.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.