Chapter 2 - Java for Beginners Course

Assignment Operator

The simple assignment operator, represented by an equals (=) sign in Java, indicates that the variable on the left will take the value that we provide on the right hand side of the = operator.

We’ve already seen some examples of this operator in previous sections as it is one of the most common operators.

To provide some further examples:

int cores = 4;
int memoryInGB = 256;
boolean externalGraphicsCard = true;

Compound Assignments

When you want to update the value of a variable as a result of an arithmetic operation, you can mix the arithmetic operator with the assignment operator for simplicity. These are called compound assignments and are represented by: +=, -=, etc.

This means that the following 2 statements are equivalent to the statement in the respective code comment:

int value = 1;

// equivalent to: value = value + 1;
value += 1;

// equivalent to: value = value * 2;
value *= 2;

System.out.println("New value is: " + value);


New value is: 4