Assignment operators in java

Let’s learn assignment operators in java.

Assignment operators in java

Assignment operators are used to assign some value to variable. It’s a combination of left and right hand side operand.

Left hand side operand of assignment operator is variable and right hand side of assignment operator is value. Right hand side operand datatype should be same as left hand side. Else compiler will throw error.

Here is the list of assignment operators,

“=” : this is the simplest assignment operator which is used to assign value to the variable on right hand side to the variable on the left. Let’s see an example,

public class AssignmentOperatorDemo 
{
   public static void main(String[] args) 
   {
      int num;
      String strName;
      num = 16;
      strName = "flowerbrackets";
      System.out.println(num);
      System.out.println(strName);
   }
}


Output:

16
flowerbrackets


“+=” : this assignment operator is a combination of ‘+’ and ‘=’ operators. This operator works by adding current value of variable on left hand side to value on right and assigning output to operand on left. Let’s see an example

public class AssignmentOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 7, b = 16;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      a += b; // a = a + b
      System.out.println("a = " + a);
   }
}


Output:

a = 7
b = 16
a = 23


“-=” : this assignment operator is a combination of ‘-‘ and ‘=’ operators. This operator works by subtracting value of variable on right hand side from current value of variable on left and assigning output to the operand on left. Let’s see an example

public class AssignmentOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 7, b = 16;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      a -= b; // a = a - b
      System.out.println("a = " + a);
   }
}


Output:

a = 7
b = 16
a = -9


“*=” : this assignment operator is a combination of ‘*’ and ‘=’ operators. This operator works by multiplying value of variable on left hand side to value on the right and assigning output to operand on left. Let’s see an example

public class AssignmentOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 7, b = 16;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      a *= b; // a = a * b
      System.out.println("a = " + a);
   }
}


Output:

a = 7
b = 16
a = 112


“/=” : this assignment operator is a combination of ‘/’ and ‘=’ operators. This operator works by dividing value of variable on left hand side by value on the right and assigning quotient to operand on left. Let’s see an example

public class AssignmentOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 21, b = 7;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      a /= b; // a = a / 7
      System.out.println("a = " + a);
   }
}


Output:

a = 21
b = 7
a = 3


“%=” assignment operator:

this assignment operator is a combination of ‘%’ and ‘=’ operators. This operator works by dividing value of variable on left hand side by value on the right and assigning remainder to operand on left. Let’s see an example

public class AssignmentOperatorDemo 
{
   public static void main(String[] args) 
   {
      int a = 21, b = 7;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      a %= b; // a = a % 7
      System.out.println("a = " + a);
   }
}


Output:

a = 21
b = 7
a = 0