What an augmented assignment operator is
An augmented assignment operator combines an arithmetic operation with an assignment. The Java language specification calls these compound assignment operators, and most tutorials say "augmented" or "shorthand". The idea is the same: myVar += 5 means "add 5 to myVar and store the result back in myVar".
Assignment in Java always evaluates the right side first, and then stores the result in the variable on the left. So the long way to add 5 is myVar = myVar + 5;. Changing a variable relative to its current value is so common that the language provides a shorter form.
int myVar = 10;
myVar += 5;
System.out.println(myVar);
15
The operators
There is one augmented operator for each arithmetic operator:
| Operator | Same as | Example with int v = 11; | New value of v |
|---|---|---|---|
+= | v = v + 4 | v += 4; | 15 |
-= | v = v - 4 | v -= 4; | 7 |
*= | v = v * 4 | v *= 4; | 44 |
/= | v = v / 4 | v /= 4; | 2 |
%= | v = v % 4 | v %= 4; | 3 |
With int values, /= is integer division: 11 divided by 4 is 2, and the remainder 3 is dropped. %= keeps exactly that remainder. Java has more of these operators for bit operations, such as &=, |=, ^=, <<= and >>=, built the same way.
The right side is evaluated completely before the operation is applied, as if it were in parentheses. After int y = 2;, the statement y *= 3 + 4; sets y to 14, not 10, because it means y = y * (3 + 4).
The += operator also works on strings, where it appends text:
String s = "n=";
s += 5 + 5;
System.out.println(s);
n=10
The right side, 5 + 5, is worked out first as the number 10, and then 10 is appended to the string.
Increment and decrement: ++ and --
Adding or subtracting exactly 1 is so frequent that Java has two further shortcuts, ++ and --. The expression x++ is equivalent to x += 1, and x-- is equivalent to x -= 1. The lesson's exercise uses them alongside the augmented operators. Strictly speaking they are called increment and decrement operators rather than augmented assignments.
The operators exist in a postfix form (x++) and a prefix form (++x). As a statement on its own, they do the same thing. The difference only shows when the expression is used inside something bigger:
int i = 5;
int j = i++;
System.out.println(i + " " + j);
i = 5;
j = ++i;
System.out.println(i + " " + j);
6 5
6 6
The postfix form hands out the old value and then increments. The prefix form increments first and hands out the new value.
A hidden cast and other gotchas
- The compiler inserts a cast.
x += yis not quitex = x + y. It meansx = (T)(x + y), whereTis the type ofx. Withint x = 5;the statementx += 3.7;compiles and leavesxequal to 8, because 8.7 is cut down to anint. The long formx = x + 3.7;is rejected withincompatible types: possible lossy conversion from double to int. The same cast letsbyte b = 10; b += 5;compile. - Integer division still truncates. After
int a = 10;,a /= 4;gives 2. Evena /= 4.0;leaves anintvariable at 2, because the result 2.5 is cut back to anint. To keep the fraction, use adoublevariable. - Overflow wraps silently. An
intholdingInteger.MAX_VALUEbecomes-2147483648after+= 1. No exception is thrown. - The order of the characters matters. The operator is
+=, not=+. The statementx =+ 5;compiles, but it assigns the value+5toxand does not add anything. - The variable on the left is read once. With
int[] arr = {10, 20, 30}; int i = 0;, the statementarr[i++] += 2;evaluatesi++only once, so it changesarr[0]to 12 and leavesiat 1. - The variable must already have a value. An augmented assignment reads the old value, so a local variable that was never assigned causes a compile error.
Which one is not an augmented assignment operator?
Quizzes often ask this. The operators +=, -=, *=, /= and %= are augmented assignments. These are not: = is plain assignment, == compares two values, + and * are ordinary arithmetic operators, and =+ is not an operator at all (it is = followed by a unary plus). The operators ++ and -- are normally listed separately as increment and decrement.
The exercise, step by step
The lesson builds one variable through a series of operations. After every step the value is easy to follow:
- Create
numberas anintwith the value 18. - Add 8 with
+=.numberbecomes 26. - Decrement with
--.numberbecomes 25. - Divide by 5 with
/=. 25 divides evenly, sonumberbecomes 5. - Increment with
++.numberbecomes 6. - Multiply by 3 with
*=.numberbecomes 18. - Subtract 18 with
-=.numberbecomes 0.
int number = 18;
number += 8;
number--;
number /= 5;
number++;
number *= 3;
number -= 18;
Printing number after each line is the easiest way to check your work: the values are 18, 26, 25, 5, 6, 18 and 0.
Key takeaways
x op= ymeansx = (T)(x op y): compute with the old value, then store the result back in the same variable.- Java has
+=,-=,*=,/=and%=for arithmetic, and similar forms for bit operations. ++and--are shortcuts for adding or subtracting exactly 1.- The built-in cast means
x += 3.7compiles for anintand drops the fraction. - With
intvariables,/=drops the remainder and%=keeps it.
