Compound assignment
Description
Operators such as += update a variable.
Explanation
score += 5 means score = score + 5.
Example
int score = 70;
score += 5;← Chapter 2 — Core Syntax & Data
mixed
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.
Operators such as += update a variable.
score += 5 means score = score + 5.
int score = 70;
score += 5;++ adds one and -- subtracts one.
Use a separate statement when clarity matters.
int attempts = 0;
attempts++;Avoid combining several increments in one expression while learning. A clear update followed by a clear use is much easier to debug.
public class UpdateDemo {
public static void main(String[] args) {
int completedLessons = 4;
completedLessons++;
System.out.println(completedLessons);
}
}