Skip to content

What is ++ in Java?

What is ++ in Java?

Quick answer

The Java ++ operator adds one to a variable. As a separate statement, i++ and ++i leave the same final value. Within an expression, postfix i++ contributes the old value, while prefix ++i contributes the new value.

For a different increment size, compare += compound assignment. The printing examples help you check the expression result separately from the variable's final value.

Separate the expression value from the stored value

Both forms of increment update a numeric variable during evaluation. The difference is the value supplied to the surrounding expression: postfix supplies the old value, while prefix supplies the incremented value. The variable is already updated before a containing assignment or method call finishes.

Save this example as IncrementDemo.java.

public class IncrementDemo {
    public static void main(String[] args) {
        int count = 5;
        int before = count++;
        int after = ++count;
        System.out.println(before);
        System.out.println(after);
        System.out.println(count);
    }
}

Output:

5
7
7

The postfix assignment stores 5 in before and leaves count at 6. The prefix expression then increments count to 7 and supplies 7 for after. Keeping those two effects separate is more useful than thinking that postfix waits until a whole statement has completed.

Use increment in a counted loop

Save this example as CounterLoop.java.

public class CounterLoop {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println(i);
        }
    }
}

Output:

0
1
2

This loop prints a value, runs the update expression, then tests the condition again. Replacing its update i++ with ++i gives the same iterations because this update clause discards the expression result. Within a larger expression, the choice can change the result.

Read multiple changes in Java evaluation order

Java specifies left-to-right evaluation of operands. When you encounter an expression with two changes to the same variable, follow that order explicitly:

Save this example as IncrementOrder.java.

public class IncrementOrder {
    public static void main(String[] args) {
        int x = 5;
        int result = x++ + ++x;
        System.out.println("result=" + result);
        System.out.println("x=" + x);
    }
}

Output:

result=12
x=7

The left operand supplies 5 and changes x to 6. The right operand changes it to 7 and supplies 7. The sum is 12. For new code, separate changes into statements when that makes the order easier to follow.

Check the variable type and shared access

Increment requires a suitable variable; a final variable cannot be changed this way. The numeric conversion rules still apply. For example, incrementing a byte at its maximum value narrows the result back to byte and wraps to its minimum value. Integer overflow does not become an automatic range check.

For a simple integer variable used as a standalone statement, i++ and i += 1 leave the same final value. A plain increment is a read, calculation and write, so it is not an atomic counter update across threads. AtomicInteger offers defined atomic methods when that is the requirement.

Questions beginners ask

Does postfix increment wait until the end of the statement?

No. It updates the variable during evaluation, but the expression supplies the old value to the surrounding operation.

Why does i = i++ leave an int variable unchanged?

The postfix operation increments the variable but supplies its old value. The assignment then stores that old value back into the variable.

Can I increment a final variable?

No. A final variable cannot be the variable changed by an increment operation.

Is i++ an atomic counter update?

No. A plain increment is not an atomic read-modify-write operation across threads. Use a suitable synchronization or atomic mechanism when updates are shared.

Use the first-program guide if the surrounding class and main method are unfamiliar. The constructor examples apply assignments while initializing objects.

Primary references

Want this checked on your own site?

Check one URL in Swiss Knife SEO without an account. A free account supports crawls of up to 100 URLs.

What does += mean in Java?

Use Java += with numbers, strings and array elements. Understand narrowing conversions and why the assignment target is evaluated once.

Read the guide ›

What is a constructor in Java?

Use Java constructors to initialize objects, overload parameter lists and share initialization. Understand when a default constructor is supplied.

Read the guide ›

What Is UUID in Java?

Work through Java UUID creation, parsing and equality examples. Understand randomness, repeatable identifiers, version checks and the limits of timestamp access.

Read the guide ›