Start learning free

LESSON 25 OF 410 · CALCULATIONS WITH NUMBERS

Augmented Assignment Operators in Java: +=, -=, *=, /=, %=

Lesson Read the full explanation

In programming, it is common to use assignments to modify the contents of a variable.

Remember that everything to the right of the equals sign (=) is evaluated first, so we can say:

myVar = myVar + 5;

to add 5 to myVar.

Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.

One such operator is the += operator:

int myVar = 10;
myVar += 5;
System.out.println(myVar); // Output: 15

This code would print 15 to the console. We call myVar += 5 an augmented assignment.

There is one such operator for every other arithmetic operator we've learned:

For subtraction: -=

int myVar = 10;
myVar -= 5; // short for myVar = myVar - 5;
System.out.println(myVar); // Output: 5

For multiplication: *=

int myVar = 3;
myVar *= 5; // short for myVar = myVar * 5;
System.out.println(myVar); // Output: 15

For division: /=

int myVar = 20;
myVar /= 4; // short for myVar = myVar / 4;
System.out.println(myVar); // Output: 5

For remainder: %=

int myVar = 11;
myVar %= 3; // short for myVar = myVar % 3;
System.out.println(myVar); // Output: 2

Assignment
Follow the Coding Tutorial and let's do some augmented assignments.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 7
    Ctrl + Enter to check

    JAVA · CALCULATIONS WITH NUMBERS

    Augmented Assignment Operators in Java, explained

    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:

    OperatorSame asExample with int v = 11;New value of v
    +=v = v + 4v += 4;15
    -=v = v - 4v -= 4;7
    *=v = v * 4v *= 4;44
    /=v = v / 4v /= 4;2
    %=v = v % 4v %= 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 += y is not quite x = x + y. It means x = (T)(x + y), where T is the type of x. With int x = 5; the statement x += 3.7; compiles and leaves x equal to 8, because 8.7 is cut down to an int. The long form x = x + 3.7; is rejected with incompatible types: possible lossy conversion from double to int. The same cast lets byte b = 10; b += 5; compile.
    • Integer division still truncates. After int a = 10;, a /= 4; gives 2. Even a /= 4.0; leaves an int variable at 2, because the result 2.5 is cut back to an int. To keep the fraction, use a double variable.
    • Overflow wraps silently. An int holding Integer.MAX_VALUE becomes -2147483648 after += 1. No exception is thrown.
    • The order of the characters matters. The operator is +=, not =+. The statement x =+ 5; compiles, but it assigns the value +5 to x and does not add anything.
    • The variable on the left is read once. With int[] arr = {10, 20, 30}; int i = 0;, the statement arr[i++] += 2; evaluates i++ only once, so it changes arr[0] to 12 and leaves i at 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:

    1. Create number as an int with the value 18.
    2. Add 8 with +=. number becomes 26.
    3. Decrement with --. number becomes 25.
    4. Divide by 5 with /=. 25 divides evenly, so number becomes 5.
    5. Increment with ++. number becomes 6.
    6. Multiply by 3 with *=. number becomes 18.
    7. Subtract 18 with -=. number becomes 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= y means x = (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.7 compiles for an int and drops the fraction.
    • With int variables, /= drops the remainder and %= keeps it.
    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free