Tuesday, October 15, 2013

Basic Operators

The Assignment Operators

There are following assignment operators supported by Java language:

Simple assignment operator ( = )

Simple assignment operator, Assigns values from right side operands to left side operand
Example :-

 class BasicOperators{
     public static void main(String[] args) {
         int x = 0;
         x = 10 + 23;
         System.out.println(x);
     }
 }

Output is 33

Add AND assignment operator ( += )

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
Example :-

 class BasicOperators{
     public static void main(String[] args) {
         int x = 10;
         x += 13;
         System.out.println(x);
     }
 }

Output is 23

Subtract AND assignment operator ( -= )

Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

 class BasicOperators{
     public static void main(String[] args) {
         int x = 10;
         x -= 3;
         System.out.println(x);
     }
 }

Output is 7 

Multiply AND assignment operator ( *= )

Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

 class BasicOperators{
     public static void main(String[] args) {
         int x = 10;
         x *= 3;
         System.out.println(x);
     }
 }

Output is 30 

Divide AND assignment operator ( /= )

Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

class BasicOperators{
     public static void main(String[] args) {
         int x = 30;
         x /= 3;
         System.out.println(x);
     }
 }

Output is 10

Modulus AND assignment operator ( %= )

Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

 class BasicOperators{
     public static void main(String[] args) {
         int x = 30;
         x %= 2;
         System.out.println(x);
     }
 }

Output is 0



No comments:

Post a Comment