Concepts / Expressions in Java

Expressions in Java

++ increments a variable by 1, and -- decrements a variable by 1.

  • Programming

Predicting a Value Before Reading the Explanation

Expressions in Java often do more than produce a value. The increment operator ++ and the decrement operator -- can also change a variable. That dual role is what makes them useful, and it is also what makes them easy to misread. Before reading on, try the example below and decide what y and x hold at the end.

java
Notes on the code
  • x starts at 5
  • x++ appears inside an assignment, so it also produces a value
  • Two print statements show the two results we need to track

What do you think happens?

After this code runs, what does y hold and what does x hold?

  • y is 5 and x is 5

  • y is 6 and x is 6

  • y is 5 and x is 6

  • y is 6 and x is 5

Reveal answer
Answer:

y is 5 and x is 6

x++ produces the old value of x, which is 5, and that value is assigned to y. The operator also increases x by 1, so x becomes 6. The expression produces one value and changes the variable at the same time.

What ++ and -- Do to a Variable

The operator ++ increments a variable by 1, and the operator -- decrements a variable by 1. This change always happens: the operator always changes the variable by 1. What varies is the value the expression produces, and that depends on where the operator is placed relative to the variable name.

Key point

An expression like x + 1 computes a value but does not change x. An expression like x++ changes x by adding 1 and also produces a value that can be used in a larger expression. Treating x++ as if it were simply x + 1 is one of the most common sources of bugs with these operators.

When you meet ++ or -- in code, ask two questions. Does the operator change the variable? Which value does the expression produce? The first answer is always yes. The second depends on whether the operator comes before or after the variable.

Post-Increment and Pre-Increment Expressions

The position of ++ determines which value the expression produces. With post-increment, written x++, the old value of x is produced first, and then x is increased by 1. With pre-increment, written ++x, x is increased by 1 first, and the new value is what the expression produces. Both forms leave x increased by 1 once the operation finishes. The difference is only the value handed back to the surrounding expression.

Expression x++Variable xstarts at 5Variable y1. read old value 52. increase x by 1 to 63. assign 5 to y
In what order do the read of x and the increment happen inside y = x++, and which value reaches y?

The diagram shows the three steps for post-increment. The old value is read, x is increased, and the old value is what gets assigned to y. Pre-increment reverses the first two steps: x is increased first, and the new value is read and assigned. Keeping this order in mind lets you predict the result of any single increment inside an expression.

java
Output
7
5
7
Notes on the code
  • b receives the old value of a, which is 5, and a becomes 6
  • ++a increases a to 7 first, so c receives the new value 7
  • The final value of a is 7 because both operators changed it
Worked example

Tracing a pre-increment assignment

Given int x = 10; and int y = ++x;, what are the values of x and y after the second line runs?

  1. 1
    Start

    x holds 10 before the expression is evaluated.

  2. 2
    Apply the pre-increment

    The ++ comes before x, so x is increased by 1 first. x now holds 11.

  3. 3
    Produce the value

    Pre-increment produces the new value, which is 11.

  4. 4
    Assign

    y receives 11.

Result

x is 11 and y is 11.

Tracking the Stored Value Through x = x++

The statement x = x++ looks like it should change x, but it does not change the final value of x. The reason is the order of operations inside the statement. The post-increment first produces the old value of x for the assignment. Although x is incremented during the operation, the assignment then stores that old value back into x. The increment's effect is overwritten before the statement ends.

x before5x after increment6x after assignment5x++ increases xassignment stores old value 5
How does the stored value of x change across the three moments of the statement x = x++?

The middle state of x briefly holds 6, but the assignment operator then writes 5 into x. The final stored value is 5, so the statement appears to do nothing. Be careful not to conclude that the increment never happened, though. The increment did occur during evaluation. It was simply overwritten by the assignment. When you debug code like this, trace both the value produced by the expression and the value stored in x.

java
Output
5
Notes on the code
  • x++ produces 5 and increases x to 6 during evaluation
  • The assignment stores 5 back into x
  • The final output is 5, the same as the starting value

Decrement Operators Follow the Same Pattern

The decrement forms follow the same post and pre distinction as the increment forms. In y = x--, y receives the old value of x, and then x is reduced by 1. In y = --x, x is reduced by 1 first, and y receives the new value. Only the direction of the change is different.

java
Output
8
10
8
Notes on the code
  • a-- produces the old value 10 for b, and a becomes 9
  • --a reduces a to 8 first, so c receives the new value 8
  • The final value of a is 8

Standalone Increment and Decrement Statements

Increment and decrement operators are easiest to read when they stand alone as statements. A statement such as counter++ clearly tells the program to increase counter. The operator is being used for its change to the variable, not for a value inside a larger expression. Because no surrounding expression needs the produced value, the post and pre forms behave the same here.

java
Output
2
4
Notes on the code
  • counter++ on its own line increases counter each time it runs
  • goalsScored moves up, down, and up again, ending one above where it started
  • No value is assigned from the operator, so the intent stays clear
Best practice

If no surrounding expression needs the value that ++ or -- produces, write the operator as a standalone statement. This keeps the intent of the line obvious to the next reader and avoids the need to trace evaluation order at all.

Mistakes Beginners Make with Increment Operators

  • Common mistake

    Treating x++ as the same as x + 1

    What it looks like
    int y = x + 1; compared with int y = x++;
    Why it fails

    The expression x + 1 computes a value but does not change x. The expression x++ changes x as well as producing a value.

    Do this instead

    When you want x changed, write x++ or x = x + 1 as a statement. When you only want a value, use x + 1 and leave x alone.

  • Common mistake

    Assuming x = x++ increases x

    What it looks like
    int x = 5; x = x++; leaves x equal to 5
    Why it fails

    The post-increment produces the old value 5, and the assignment stores that value back into x, overwriting the increment.

    Do this instead

    If you want x to grow by one, write x++; as a standalone statement.

  • Common mistake

    Reading only the assignment when tracing y = x++

    Why it fails

    This tracks y but misses the change to x, so you may wrongly conclude that x stayed unchanged.

    Do this instead

    Trace two separate results: the value produced by the expression, which goes to y, and the new value stored in x.

  • Common mistake

    Mixing up pre and post positions

    What it looks like

    Expecting y = ++x to give y the old value

    Why it fails

    Pre-increment changes x first and produces the new value.

    Do this instead

    Read the operator position first: a leading ++ means the new value, a trailing ++ means the old value.

Practice Tracing Increment Expressions

MEDIUM

Given int m = 4; int n = m--; int p = --m; what are the final values of m, n, and p? Trace the operator position and the change to m for each line before you check your answer.

Hints
  • For n = m--, n receives the old value of m, and m is reduced after that.
  • For p = --m, m is reduced first, and p receives the new value.
  • Track m separately from the values assigned to n and p.

Key Ideas to Remember

  1. 1
    ++ increments a variable by 1, and -- decrements a variable by 1. The change to the variable always happens.
  2. 2
    x++ produces the old value and then increases x; ++x increases x first and produces the new value.
  3. 3
    y = x++ assigns the old value to y and leaves x one higher, while y = --x reduces x first and assigns the new value to y.
  4. 4
    x = x++ leaves the final value of x unchanged, because the assignment stores the old value back into x.
  5. 5
    When the produced value is not needed, use ++ or -- as a standalone statement such as counter++ to keep intent clear.

Key Takeaways

  • ++ increments a variable by 1 and -- decrements a variable by 1; the change to the variable always occurs.
  • Post-increment x++ produces the old value, while pre-increment ++x produces the new value.
  • Trace two results for any expression containing ++ or --: the value the expression produces and the value stored in the variable.
  • x = x++ does not change the final value of x because the assignment overwrites the incremented value with the old value.
  • Standalone statements such as counter++ keep the intent of an increment clear.