Assignment Operators
In Java programming, assignment operators help you set or change the value of a variable. These operators combine assignments with other operations.
Basic assignment
Here's a simple example:
int length = 20;
int z = 5;
Compound assignment operator
There are operators that perform an operation and then assign the result back to the variable. Let's look at compound assignment operators.
+=
-=
*=
/=
%=
Here is an example:
public class Operators {
public static void main(String[] args) {
int value = 10;
System.out.println(value += 5); // (10 + 5) = 15
System.out.println(value -= 3); // (15 - 3) = 12
System.out.println(value *= 3); // (12 * 3) = 36
System.out.println(value /= 5); // (36 / 5) = 7
System.out.println(value %= 3); // (7 % 3) = 1
}
}