Assignment Operators in Java with Example

Assignment Operators in Java is used to assign a value to the variable. The assignment operators(=) has right to left associativity. In many cases, the assignment operator can be combined with another operator to make a shorter version of a statement called a compound statement. for example, instead of =a+10, we can write a+=10.

In Java, we can use many assignment operators such as:

1. +=, This operator is used for adding left operand with right operand and then assigning it to a variable on the left.

2. -=,.This operator is used for subtracting left operand with right operand and then assigning it to a variable on the left.

3. *=, This operator is used for multiplying left operand with right operand and then assigning it to a variable on the left.

4. /=, This operator is used for dividing left operand with right operand and then assigning it to a variable on the left.

5. %=, This operator is used for modulo left operand with right operand and then assigning it to a variable on the left.


Example of Assignment Operators in Java:


class AssignmentOperator{  
   public static void main(String[] args){  
     int a = 20;  
     a += 3;  //20+3  
     System.out.println(a);  
     a -= 4;  //23-4  
     System.out.println(a);  
     a *= 2;  //19*2  
     System.out.println(a);  
     a /= 2;  //38/2  
     System.out.println(a); 
     a %= 2;  //19%2
     System.out.println(a);
    }
}  


Output:




You may also like these related posts:

1. Unary Operators in Java with example.
2. Arithmetic Operators in Java with example.
3. Basic data types in java with example. 
4. Java Hello World program - Java first program.
5. How to set path in java - Java Environment set up.
6. Top 100 java interview questions and answers.

Assignment Operators in Java with Example Assignment Operators in Java with Example Reviewed by Prashant Srivastava on December 14, 2019 Rating: 5

No comments:

Powered by Blogger.