Decrement Assign Operator

Increment Assign Operator in the previous example is a very common compound assign operator in Java. The next most common involves subtraction. What we want to do is to decrement the value of a variable, and assign it back to the variable - hence the name decrement assign. Below are some examples that we will share on how to perform decrement assign operator in Java.

Decrement Assign With Literal

The symbol that we need to use if we want to do decrement assign is -=. It is simple to understand and remember as the minus sign is the symbol for decrement or subtraction, which = sign is the symbol we use for assignment - hence decrement assign. Below is a very simple example on how to use decrement assign with a literal in Java:
int a = 12;
a -= 4;
System.out.println(a);
So since this is a decrement assign operator, the first operation is the decrement, where we subtract 4 from the value of a. Then the answer is assigned back to a. Hence, when we inspect the value of variable a, we get:
8

Decrement Assign With Another variable

We can also decrement a variable with another variable using the decrement assign operator. Below is an example on how to do that:

int a = 25;
int b = 10;
a -= b;
System.out.println(a);

So we start with a having the value of 25, and subtract the value of b from it, which is 10. The result should be 15, which should be the content of variable a. Hence displaying the value of a will result to:
15

Decrement Assign With An Expression

Generally, we can decrement a variable with an expression from the right. Below is a simple example on how to do that:
int a = 50;
int b = 2;
a -= b * 5;
System.out.println(a);
So b has the value of 2 and we multiply it with 5 to get 10. We do this because we evaluate first the expression from the right of the decrement assign operator. Then a has the value of 50 initially, and we deduct the value of the expression which is 10, to get the result of 40. So we now know the content of variable a, which is shown below:
40