Assignment Operators in Java

An assignment operator is used to assign a value to a variable on the left hand side of the operator. There are 12 assignment operators. Out of which one is simple assignment operator and the rest 11 are compound assignment operators.

  1. Simple Assignment Operator (=)
  2. Compound Assignmnet Operator (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=)
OperatorTypeDescription
=-x = 5, assign value 5 to variable x.
+=Arithmaticx += y same as x = x+y
-=Arithmaticx -= y same as x = x-y
*=Arithmaticx *= y same as x = x*y
/=Arithmaticx /= y same as x = x/y
%=Arithmaticx %= y same as x = x%y
&=Bitwise, booleanx &= y same as x = x&y
|=Bitwise, booleanx |= y same as x = x|y
^=Bitwise, booleanx ^= y same as x = x^y
<<=Bitwisex <<= y same as x = x << y
>>=Bitwisex >>= y same as x = x>>y
>>>=Bitwisex >>>= y same as x = x>>>y
  • +=, -=, *=, /=, %= are used in arithmatic expressions,
  • &=, |=, ^= are used in both bitwise and boolean logical expressions and
  • <<=, >>=, >>>= are used only in bitwise operations.

1. Simple Assignment Operator

Alone assignement operator(=) is called as simple assignment.

Output :

You can use this operator to assign any type of data(even objects) as long as both sides of the assignment operator are type compatible with each other. If they are not type compatible with each other, it would cause compilation error.


1.1 Chained Assignment

You can use a number of assignments one after the other in a single statement. like x=y=z=5 . This is equivalent to x=(y=(z=5)).

Lets see how it works:

  • First it sets z=5, and returns 5 because return value of an assignment is the value being assigned,
  • This returned value(5) is then assigned to y, and the assigned value(5) is returned.
  • This chain can go on and on.

2. Compound Assignment Operator

An assignment operator of the form op= is known as a compound assignment operator. Here op can be any arithmatic, bitwise or boolean operator like +>> or &.

Here are the details about them in short:

  • The expression x op= y  is equivalent to x = x op y .
  • There are 12 assignment operators in total. Out of which 11 are compound assignemnt operators. All assignment operators are binary i.e they take two arguments.
  • &=, |=, ^= are overloaded to work like both bitwise operators and boolean operators.
  • Not all operators have compound assignment forms. For example && or ||.

All these binary assignment operators(op=) work the same way.

  • The variable on the left is taken and its value is saved.
  • Value/ variable on the right is taken and the binary operator (op) is applied on the two.
  • The result is assigned to the variable on the left hand side of the operator.

2.1 An Example

Output :


2.2 No explict type casting is required with compound assignment operators

In case of compound assignment operators, no type casting is required. In fact it is implicit and handled by the compiler. So expression x op= y is equivalent to x = (T)(x op y) where T is the type of x.

Output :

  • Note that if you use x = x op y then you may need to use explicit type casting if the result is wider than the type of x.
  • In case of x op= y, type is handled by the compiler itself.


3. Miscellaneous Points to remember

3.1 Return value of an assignment operator is the value being assigned

The result of an assignment operation is the value being assigned to the variable on the left hand side of assignment operator. It works the same for all the assignment operators.

Output :

 


3.2 All assignment operators are right associative

All assignment operators are right associative. So a statement of the form x=y=z=5 is evaluated as x=(y=(z=5)).

Output :

 


3.3 Left hand side of the assignment can be any reference variable including array, or members of a objects.

Output :

Note that

  • After assigning Students to Citizens, both have same reference value.
  • After assigning f1 to f2, both have same reference value.
  • After assigning Students[0] = “Tommy”, its value has been changed to “Tommy” from “Tom“.
  • After assigning f1.taste =”Sweet“, its value has become “Sweet“.