🌱BeginnerOperators

Assignment Operators

Use compound assignment operators (+=, -=, *=, /=, %=) as shortcuts for updating variable values.

Overview

Assignment operators store a value into a variable. The basic assignment operator is =. Compound assignment operators combine an arithmetic operation with assignment, letting you update a variable with less typing: x += 5 is exactly the same as x = x + 5. These operators improve code readability by making it clear that you are updating an existing variable, not computing a brand-new value.

Key Points

  • •= basic assignment: `x = 10;`
  • •+= add and assign: `x += 5;` → `x = x + 5;`
  • •-= subtract and assign: `x -= 3;` → `x = x - 3;`
  • •*= multiply and assign: `x *= 2;` → `x = x * 2;`
  • •/= divide and assign: `x /= 4;` → `x = x / 4;`
  • •%= modulus and assign: `x %= 3;` → `x = x % 3;`

Examples

Compound assignment operators in action

public class Main {
    public static void main(String[] args) {
        int score = 100;

        score += 25;   // score = 100 + 25 = 125
        System.out.println(score); // 125

        score -= 10;   // score = 125 - 10 = 115
        System.out.println(score); // 115

        score *= 2;    // score = 115 * 2 = 230
        System.out.println(score); // 230

        score /= 5;    // score = 230 / 5 = 46
        System.out.println(score); // 46

        score %= 7;    // score = 46 % 7 = 4
        System.out.println(score); // 4
    }
}

Each compound operator modifies the variable in place - much cleaner than score = score + 25.

Accumulating values in a loop (typical use case)

public class Main {
    public static void main(String[] args) {
        int total = 0;

        // Accumulate sales for the week
        total += 150; // Monday
        total += 200; // Tuesday
        total += 175; // Wednesday
        total += 220; // Thursday
        total += 195; // Friday

        System.out.println("Weekly total: $" + total); // $940
    }
}

The += pattern is the standard way to accumulate a running total.

String compound assignment

public class Main {
    public static void main(String[] args) {
        String message = "Hello";
        message += ", World"; // String concatenation
        message += "!";
        System.out.println(message); // Hello, World!

        // Building a sentence word by word
        String sentence = "";
        sentence += "Java ";
        sentence += "is ";
        sentence += "fun!";
        System.out.println(sentence); // Java is fun!
    }
}

+= also works on Strings for concatenation (but StringBuilder is more efficient in loops).

Common Mistakes

  • Confusing = (assignment) with == (equality check) inside conditions
  • Expecting /= to produce a decimal result when both operands are int: `x /= 3` with int x is still integer division
  • Using compound operators on final variables - you cannot update a constant with +=

Note

Prefer compound assignment operators when updating a variable. They are shorter, less prone to typos (no chance of writing x = y + 5 when you meant x = x + 5), and communicate intent more clearly.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What does `x += 7;` do when x = 10?