Java Assignment Operator
The assignment operator (=) assigns the value on its right to the operand on its left. For example:
a = b;
Here we are assigning the value of the variable b to the variable a. What we are not saying is a is equal to b – that is something you might think if you were not familiar with programming because in mathematics that’s what a=b means.
In Java, to say a value is equal to another value requires a double equals sign, or ==. This a common point of confusion for new programmers and it’s important to understand the difference between the assign (=) and the equal (==) operator.
Let’s have a look at some uses and variations of the assignment operator:
Operator | Example | Equivalent |
---|---|---|
= | a = b | a = b |
+= | a += b | a = a + b |
-= | a -= b | a = a – b |
*= | a *= b | a = a * b |
/= | a /= b | a = a / b |
%= | a %= b | a = a % b |