CHAPTER 4 ■ OPERATORS
Table 4-3. The assignment operators
Operator Description
= This is the basic assignment operator.
+= Add the right-hand operand to the left-hand operand and assign the result to the left-hand
operand. x += 2 is the same as x = x + 2.
-= Subtract the right-hand operand from the left-hand operand and assign the result to the left-
hand operand. x -= 2 is the same as x = x – 2.
= Multiply the right-hand operand by the left-hand operand and assign the result to the left-
hand operand. x = 2 is the same as x = x * 2.
/= Divide the right-hand operand by the left-hand operand and assign the result to the left-hand
operand. x /= 2 is the same as x = x / 2.
%= Use the right-hand operand to determine the modulus of the left-hand operator and assign
the result to the left-hand operator. x %= 2 is the same as x = x % 2.
&= Perform a bitwise and operation and assign the result to the left-hand operator. x &= y is the
same as x = x & y.
|= Perform a bitwise inclusive operation and assign the result to the left-hand operator. x |= y is
the same as x = x | y.
^= Perform a bitwise exxclusive or operation and assign the result to the left-hand operator. x ^=
y is the same as x = x ^ y.
<<= Perform a bitwise left-shift operation and assign the result to the left-hand operator. x <<= y
is the same as x = x << y.
= Perform a bitwise right-shift operation and assign the result to the left-hand operator. x &= y
is the same as x = x & y.
= Perform a bitwise signed right-shift operation and assign the result to the left-hand operator.
x &= y is the same as x = x & y.
Comparing and Sorting Objects
As I indicated previously, comparing objects differs from comparing primitives. The comparison
operators work for primitives, but they do not work for objects. Instead, Java requires the use of a
number of different interfaces and methods to compare objects.