Expressions in Java
++ increments a variable by 1, and -- decrements a variable by 1.
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.
- 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?
Reveal 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.
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.
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.
7
5
7- 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
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?
- 1Start
x holds 10 before the expression is evaluated.
- 2Apply the pre-increment
The ++ comes before x, so x is increased by 1 first. x now holds 11.
- 3Produce the value
Pre-increment produces the new value, which is 11.
- 4Assign
y receives 11.
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.
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.
5- 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.
8
10
8- 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.
2
4- counter++ on its own line increases counter each time it runs
goalsScoredmoves up, down, and up again, ending one above where it started- No value is assigned from the operator, so the intent stays clear
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 likeint y = x + 1; compared with int y = x++;Why it failsThe expression x + 1 computes a value but does not change x. The expression x++ changes x as well as producing a value.
Do this insteadWhen 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 likeint x = 5; x = x++; leaves x equal to 5Why it failsThe post-increment produces the old value 5, and the assignment stores that value back into x, overwriting the increment.
Do this insteadIf 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 failsThis tracks y but misses the change to x, so you may wrongly conclude that x stayed unchanged.
Do this insteadTrace 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 likeExpecting y = ++x to give y the old value
Why it failsPre-increment changes x first and produces the new value.
Do this insteadRead the operator position first: a leading ++ means the new value, a trailing ++ means the old value.
Practice Tracing Increment Expressions
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++ increments a variable by 1, and -- decrements a variable by 1. The change to the variable always happens.
- 2x++ produces the old value and then increases x; ++x increases x first and produces the new value.
- 3y = 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.
- 4x = x++ leaves the final value of x unchanged, because the assignment stores the old value back into x.
- 5When 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.