Assignment Operators
Assignment operators allow us to assign values to our objects. We've used this operator many times throughout our chapters so far—it's one of the most fundamental operations in programming, but as always, there's more that we can learn about these operators.
The most basic assignment operator is where we take a value and assign it to an object, as follows:
int myInt = 5;
We're familiar with this, but what we might not be familiar with is the concept of combining these with arithmetic operators. Let's imagine a scenario where we need to increment a value by 5. We could do this as follows:
myInt = myInt + 5;
We take the value of myInt
, add 5
to it, and then assign it back to the original variable. We can do this in a more refined way, however, by combining the two operators together. The assignment operator can be preceded by an arithmetic operator to achieve...