Java: Assignment Operators

assignment operator

Operators

Java provides quite a variety of operators which come in handy to the programmer for the manipulation of variables.

The operators in java are as follows:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
Here we will learn more about Assignment Operators in Java with the help of their syntax and example
Assignment operator

Assignment Operators

Assignment operators are used to assigning the value on its right to the variable on its left.

OperatorFunctionalitySyntax
= (simple assignment)Assigns values from right side operands to left side operand.m=n
+= (compound of  + and =)It adds the right operand to the left operand and assigns the result to the left operand.m+=n
-=(compound of  – and =)It subtracts the right operand from the left operand and assigns the result to the left operand.m-=n
*= (compound of  * and =)It multiplies the right operand with the left operand and assigns the result to the left operand.m*=n
/= (compound of  / and =)It divides the left operand with the right operand and assigns the result to the left operand.m/=n
%= (compound of  % and =)It takes modulus using two operands and assigns the result to the left operand.m%=n

Syntax

Run
public class Main {

public static void main(String args[]) {
int m = 10, n = 20, o = 0;

o = m + n;
System.out.println("o = m + n : " + o );

o += m ;
System.out.println("o += m : " + o );

o -= m ;
System.out.println("o -= m : " + o );

o *= m ;
System.out.println("o *= m : " + o );

m = 10;
o = 15;
o /= m ;
System.out.println("o /= m = " + o );

m = 10;
o = 15;
o %= m ;
System.out.println("o %= m = " + o );

}

}

Output

o = m + n = 30
o += m  = 40
o -= m = 30
o *= m = 300
o /= m = 1
o %= m = 5