The assignment operator (=), assigns a value on its left side to the variable on the right side.
Assign the value 100 to the variable total
int total = 100;
Change the value of the variable by assigning it different values
boolean isLoggedIn = false; //change the value once user is is logged in sucessfuly isLoggedIn = true;
Multiple Variable Assignment
You can also assign the same value to multiple variables by chaining the assignment operator(=)
int x; int y; x = y = 10; //Both x and y will have the value 10
Compound Assignment Operators
The assignment operator can be used by other operators as a shortcut to perform an operation.
Here is a common operation, adding 1 to the current value
int sum = 10; sum = sum + 1; //Sum is now 11
Using a compound += to do the same operation and yield the same results
int sum = 10; sum += 1; //Sum is now 11
Compound Assignment Operators Table
|+=||Compound Addition||sum += 10|
|-=||Compound Subtraction||sum -= 10|
|*=||Compound Multiplication||sum *= 10|
|/=||Compound Division||sum /= 10|
|%=||Compound Modulo||sum %= 10|
Add 10 the current value of sum. sum will be 20.
int sum = 10; sum += 10;
Multiply with 10 to the current value of sum. The value of sum will now be 100.
int sum = 10; sum *= 10;