Java: Assignment Operators

assignment operator

Assignment Operator in Java

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

 

Here we will learn more about Assignment Operators in Java with the help of their syntax and example

Assignment Operators

Assignment operators are used to assigning the value on its right to the variable on its left.
Operator Functionality Syntax
= (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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription