← Chapter 2 — Core Syntax & Data

11. Assignment & Increment Operators

mixed

Description

Assignment stores a value in a variable. Compound assignments combine an operation with assignment, which can make an update concise.

Increment and decrement change a numeric value by one. Prefix and postfix forms differ in when the old or new value is used in a larger expression.

Subtopics

Compound assignment

Description

Operators such as += update a variable.

Explanation

score += 5 means score = score + 5.

Example

int score = 70;
score += 5;

Increment

Description

++ adds one and -- subtracts one.

Explanation

Use a separate statement when clarity matters.

Example

int attempts = 0;
attempts++;

Explanation

Avoid combining several increments in one expression while learning. A clear update followed by a clear use is much easier to debug.

Example

public class UpdateDemo {
    public static void main(String[] args) {
        int completedLessons = 4;
        completedLessons++;
        System.out.println(completedLessons);
    }
}

Key points

  • = assigns a value.
  • += combines addition and assignment.
  • ++ changes a value by one.

Open reference