Arithmetic Assignment operators in Java
The five arithmetic assignment operators are a form of short hand. Various textbooks call them "compound assignment operators" or "combined assignment operators". Their usage can be explaned in terms of the assignment operator and the arithmetic operators.
| Compound Operator | Sample Expression | Expanded Form |
|---|---|---|
| += | x += 2 | x = x + 2 |
| -= | y -= 6 | y = y - 6 |
| *= | z *= 7 | z = z * 7 |
| /= | a /= 4 | a = a / 4 |
| %= | b %= 9 | b = b % 9 |
Code Snippet
//Arithmetic Assignment operators in Java
public class App {
public static void main(String args[]) {
int a = 123;
System.out.println(a);
a += 10;
System.out.println(a);
a -= 10;// a=a-10
System.out.println(a);
a *= 10;
System.out.println(a);
a /= 10;
System.out.println(a);
a %= 10;
System.out.println(a);
}
}