Java Arithmetic Compound Operator
Compound Assignment Operators in Java
Java gives special operators to combine an arithmetic operator with an assignment and which is known as Compound Assignment Operator. This session describes how it works and its benefits.
Consider the following addition and subtraction statements,
a = a + 5;
b = b - 5;
a += 5;
b -= 5;
Compound Assignment operators are available for all arithmetic and binary operators.
Syntax:
General Form:
Compound assignment Operator Form:
Examples:
|
Compound Assignment Operator Form |
a = a - 5; | a -= 5; |
a = a + 5; | a += 5 |
a = a % 5; | a %= 5 |
a = a / 5 | a /= 5 |
a = a * 5 | a *= 5 |
Advantages of Compound Assignment Operators
The Compound Assignment Operators are used by Java Professionals to attain two major benefits.
1. Compound Assignment Operators are shorthand and saves a few letters every time when you type Java Code.
2. It is efficient than its equivalent long form in some cases.