Divide Assign Operator

The divide assign operator is a short hand notation provided by Java so we could shorten our code where we want to divide a variable by some value and assign back the result to the variable. It is two operations in one statement to make our code much shorter, concise, and readable. Below are some examples on how we can use Divide Assign Operator in Java.

Divide Assign With Literal

Take note of the operator /= as this combination of symbol denotes the divide assign operator. Since we are doing two operations into one, which involves division and then assignment, it is intuitive that we combine the symbol for division and assignment to form this operator. Below is a simple example involving literal value with divide assign operator:
int a = 20;
a /= 5;
System.out.println(a);
So variable a is divided by 5, which is 20 divided by 5, that will result into the value 4. We then assign this quotient to the variable a. So below is the output when we try to display the contents of variable a:
4

Divide Assign With Another variable

what if we want to divide a variable by something we don't know before hand? Then we can use a variable with divide assign operator as shown below:

int a = 70;
int b = 7;
a /= b;
System.out.println(a);

Variable a is divided by variable b, which is simply 70 divided by 7. The quotient, which is 10, is assigned to a. Hence we get below output:
10

Divide Assign With An Expression

And to make it official, it is actually supported to have any expression on the right side of the divide assign operator. For example:
int a = 24;
int b = 3;
a /= b * 2;
System.out.println(a);
We perform first the operation on the right side of the divide assign operator, wich is b multiplied by 2, resulting 6. Then we divide the variable a with 6, to give us the quotient 4. That value is assigned back to a, and thus we get below result:
4