Assignment and evaluation order
What you will learn
Expressions may compute a value, change stored state, or do both. Assignment updates a variable; increment and decrement update it by one. Knowing both the returned expression value and the new stored value helps prevent errors in compact calculations.
public class Main { public static void main(String[] args) { int stock = 40; stock -= 9; stock += 3; System.out.println(stock); byte counter = 125; counter += 5; System.out.println(counter); }}Expected output:
34-126The stock updates normally. The byte operation narrows the computed int 130 back to byte.
How it works
= stores the right-hand value after applicable assignment conversion. +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>= combine an operation with assignment. A compound assignment also converts the computed result back to the left-hand type and evaluates the target only once. ++x increments x and yields its updated value; x++ increments x but yields the prior value. The same distinction applies to --. As standalone statements, prefix and postfix have the same final effect. Precedence controls grouping: multiplication before addition, comparisons before conditional AND, AND before OR. Associativity resolves same-precedence grouping (10 - 3 - 2 groups left, chained assignment groups right). Java evaluates operands left-to-right, but that is not a rule that addition executes before multiplication. Parentheses clarify grouping. Short-circuit and conditional expressions can skip operands. Avoid combining multiple changes to one variable in a single expression when simple statements express intent more clearly.
public class Main { public static void main(String[] args) { int ticket = 7; int issued = ticket++; int next = ++ticket; System.out.println(issued); System.out.println(next); System.out.println(ticket--); System.out.println(--ticket); }}Expected output:
7997Postfix returns the old value while prefix returns the new one. The final stored ticket is 7.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { System.out.println(4 + 3 * 2); System.out.println((4 + 3) * 2); System.out.println(10 - 3 - 2); int x = 2; int result = x++ * 10 + x; System.out.println(result); System.out.println(x); }}Expected output:
10145233Multiplication groups before addition. The left operand reads 2 and increments x before the right operand reads 3. This last expression is for tracing, not a recommended style.
Common mistakes
count = count++ leaves count unchanged: the postfix expression increments it, then assignment writes back the old value. Use count++ or count += 1 as a standalone statement. Do not assume byte b = 120; b += 10; rejects overflow; narrowing is built into compound assignment.
Try it yourself
A kiosk starts with ticket number 100. Issue that ticket, advance the counter, then reserve three further numbers with compound assignment. Print the issued number and the new counter. Repeat with a starting number of 0; stay within the int range.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Assignment and evaluation order — knowledge check
Apply assignment and evaluation order to examples and boundary conditions.
After `int x = 5; int y = x++;`, what are x and y?
What is `2 + 5 * 3`?
What happens to a byte b initialized to 127 after `b += 1`?
What is the final value after `int x = 9; x = x++;`?
What does Java guarantee for ordinary binary operand evaluation?