Assignment Operator in Java with Example

Assignment operator is one of the simplest and most used operator in java programming language. As the name itself suggests, the assignment operator is used to assign value inside a variable. In java we can divide assignment operator in two types :

  • Assignment operator or simple assignment operator
  • Compound assignment operators

What is assignment operator in java

The = operator in java is known as assignment or simple assignment operator. It assigns the value on its right side to the operand(variable) on its left side. For example :

 int a = 10; // value 10 is assigned in variable a
 double d = 20.25; // value 20.25 is assigned in variable d
 char c = 'A'; // Character A is assigned in variable c
 a = 20; // variable a is reassigned with value 20

The left-hand side of an assignment operator must be a variable while the right side of it should be a value which can be in the form of a constant value, a variable name, an expression, a method call returning a compatible value or a combination of these.

 int a = 10, b = 20;  // right side can be a constant value
 int c = a; // right side can be a variable
 int d = a+b; // right side can be an expression
 int e = sum(a,b) // right side can be a method(sum) call
 // Following are incorrect declarations
 a + b = 20; // Left side of = operator can't be an expression
 1 = a; // Left side of = operator must be a variable

The value at right side of assignment operator must be compatible with the data type of left side variable, otherwise compiler will throw compilation error. Following are incorrect assignment :

 int a = 2.5; // value 10.5 can't be assigned in int variable
 char c = 2.5; // value 2.5 can't be assigned in char variable

Another important thing about assignment operator is that, it is evaluated from right to left. If there is an expression at right side of assignment operator, it is evaluated first then the resulted value is assigned in left side variable.

 int a = 10, b = 20, c = 30;
 int x = a + b + c;  // value of x will be 60
 a = b = c; // value of a and b will be 30

Here in statement int x = a + b + c; the expression a + b + c is evaluated first, then the resulted value(60) is assigned into x. Similarly in statement a = b = c, first the value of c which is 30 is assigned into b and then the value of b which is now 30 is assigned into a.

The variable at left side of an assignment operator can also be a non-primitive variable. For example if we have a class MyFirstProgram, we can assign object of MyFirstProgram class using = operator in MyFirstProgram type variable.

 MyFirstProgram mfp =  new MyFirstProgram();     

Is == an assignment operator ?

No, it's not an assignment operator, it's a relational operator used to compare two values.

Is assignment operator a binary operator

Yes, as it requires two operands.

Assignment operator program in Java

 class AssignmentOperator {
    public static void main (String[] args) {
       int a = 2; 
       int b = a; 	
       int c = a + b;
       int d = sum(a,b);
       boolean e = a>b;
       System.out.println("a = " + a);
       System.out.println("b = " + b);
       System.out.println("c = " + c);
       System.out.println("d = " + d);
       System.out.println("e = " + e);
    }
    static int sum(int x, int y) {
       return x+y;
    }    
  } 

Output:

a = 2
b = 2
c = 4
d = 4
e = false

Java compound assignment operators

The assignment operator can be mixed or compound with other operators like addition, subtraction, multiplication etc. We call such assignment operators as compound assignment operator. For example :

 int a = 10, b = 20;
 a += 10;  // is same as a = a + 10; // += is a compound assignment
 b *= 5;   // is same as b = b * 5; // *= is a compound assignment

Here the statement a += 10; is the short version of a = a + 10; the operator += is basically addition compound assignment operator. Similarly b *= 5; is short version of b = b * 5; the operator *= is multiplication compound assignment operator. The compound assignment can be in more complex form as well, like below :

 a -= a+b;  // is same as a = a - (a+b);
 b *= a*b+c;   // is same as b = b * (a*b+c);

List of all assignment operators in Java

The table below shows the list of all possible assignment(simple and compound) operators in java. Consider a is an integer variable for this table.

Operator Example Same As
= a = 10 a = 10
+= a += 5 a = a + 5
-= a -= 3 a = a - 3
*= a *= 6 a = a * 6
/= a /= 5 a = a / 5
%= a %= 7 a = a % 7
&= a &= 3 a = a & 3
|= a |= 3 a = a | 3
^= a ^= 2 a = a ^ 2
>>= a >>= 3 a = a >> 3
>>>= a >>>= 3 a = a >>> 3
<<= a <<= 2 a = a << 2

How many assignment operators are there in Java ?

Including simple and compound assignment we have total 12 assignment operators in java as given in above table.

What is shorthand operator in Java ?

Shorthand operators are nothing new they are just a shorter way to write something that is already available in java language. For example the code a += 5 is shorter way to write a = a + 5, so += is a shorthand operator. In java all the compound assignment operator(given above) and the increment/decrement operators are basically shorthand operators.

Compound assignment operator program in Java

 class CompoundAssignmentOperator {
    public static void main (String[] args) {
       int a = 10;
       a += 10;
       int b = 100;
       b -= 20;
       int c = 3;
       c *= 10;      
       short s = 200;
       s &= 100;
       short s2 = 100;
       s2 ^= 10;
       byte b2 = 127;       
       b2 >>= 3;
        
       System.out.println("a = " + a);
       System.out.println("b = " + b);
       System.out.println("c = " + c);       
       System.out.println("s = " + s);
       System.out.println("s2 = " + s2);
       System.out.println("b2 = " + b2);
    }    
  } 

Output:

a = 20
b = 80
c = 30
s = 64
s2 = 110
b2 = 15

What is the difference between += and =+ in Java?

An expression a += 1 will result as a = a + 1 while the expression a =+ 1 will result as a = +1. The correct compound statement is +=, not =+, so do not use the later one.