Monday, 8 February 2016

JAVA Operators

An operator is a symbol which operates on operand(s)

Example:
     a+b
     a, b are the operands
    + is a symbol
In java we have many operators. Those are

1.       Increment and decrement operator

    We can call these operators as unary operators. These are operating on single operand.

    Example:

        public class Unary_Operator {
       public static void main(String[] args)
         {
            int i=10;
               System.out.println("pre increment:"+i++);
               System.out.println("post increment:"+ ++i);
               System.out.println("pre decrement:"+i--);
               System.out.println("post decrement:"+--i);
         }
}

Output:
   pre increment:10
   post increment:12
   pre decrement:12
   post decrement:10

2.       Shift operator:   These operators are used for gaming programs. We have left shift (<<) and right shift (>>) operators. These are directly performing operations on bits. These operators we can call it as unary operator as it takes only one operand

Example:
      public class Shift_Operators {
             public static void main(String[] args){
                 int i=8;
                 System.out.println("Right shift of "+i+" by 1: value->"+(i>>1));
                 System.out.println("Left shift of "+i+" by 1: value->"+(i<<1));
                }
   }
Output:

  Right shift of 8 by 1: value->4
 Left shift of 8 by 1: value->16

3.  Arithmetic operator: These operators will take two operands. These operators we can call it as binary operator.
   Example:
   
        public class Arithmetic_Operators {
       public static void main(String[] args){
                 int i=10;
                 int j=8;
                 System.out.println("Addition operator: "+(i+j));
                 System.out.println("Subtraction operator: "+(i-j));
                 System.out.println("Division operator: "+(i/j));
                 System.out.println("mod operator: "+(i%j));
                 System.out.println("Multiplication operator: "+(i*j));
         }
}

Output:
       Addition operator: 18
Subtraction operator: 2
Division operator: 1
mod operator: 2
Multiplication operator: 80

4.  Relational operator: These operators will take two operands. These operators we can call it as binary operator. These operators we will use for decision making in flow control. We have below relational operators. These operators will returns Boolean values true or false
   
    <, <=, >, >=, ==
  Example:

    public class Relational_Operators {
       public static void main(String[] args){
                 int i=10;
                 int j=8;
                 System.out.println("Lessthan operator: "+(i<j));
                 System.out.println("greatherthan operator: "+(i>j));
                 System.out.println("Lessthan or equals operator: "+(i<=j));
                 System.out.println("greatherthan or equals operator: "+(i>=j));
                 System.out.println("equals operator: "+(i==j));
         }
}

Output:
Lessthan operator: false
greatherthan operator: true
Lessthan or equals operator: false
greatherthan or equals operator: true
equals operator: false

5.       Bitwise operator: These operators are directly operates on bits. These will take 2 operands. We can apply these operators on primitive and Boolean valuesThose are

Bitwise AND (&): If at least one operand is false then result will be false

                          True & True = true
                          True & false = false
                          False & true = false
                          False & false = false

Bitwise OR (|):  If at least one operand is true the result will be true

           True | True = true
                          True | false = true
                          False| true = true
                          False | false = false

Bitwise X-OR (^): If both operands are same then the result will be true

           True ^ True = true
                          True ^ False = false
                          False ^ True = false
                          False ^ false = true
Example:

    public class Bitwise_Operators {
       public static void main(String[] args){
                 int i=10;
                 int j=8;
                 System.out.println("Bitwise AND operator: "+(i&j));
                 System.out.println("Bitwise OR operator: "+(i|j));
                 System.out.println("Bitwise XOR operator: "+(i^j));
    }
}

Output:
 
      Bitwise AND operator: 8
    Bitwise OR operator: 10
    Bitwise XOR operator: 2


6.       Short circuit operator: The purpose of short circuit operators in order to improve the performance of the system by skipping some unnecessary operations. Those are AND (&&), OR (||), NOT (!) . these operators are apply only on Booleans
Example:
public class Shortcircuit_Operators {
       public static void main(String[] args){
                 int i=10;
                 int j=8;
                 System.out.println((i>j)||(i>j/10));
                 System.out.println((i>j)&&(i>j/10));
       }
}

Output:
   true
   false

7.       Instanceof operator: This operator is for checking whether the created object is related to same class or not. This will returns Boolean value
Example:
public class InstanceOf_Operator {
              public static void main(String[] args){
                 Student s = new Student();
                 System.out.println(s instanceof Student);
                System.out.println(s instanceof Parent);
              }
}   
class Student{
}
Class Parent{
}
    Output:
              true
              false
8.      new operator: This operator is used for creating a object
Example:
   public class InstanceOf_Operator {
              public static void main(String[] args){
                 Student s = new Student();
              }
}   
class Student{
}
9.       typecast operator: This operator is used for converting one data type to another data type.
Example:

    public class TypeCast_Operator {
       public static void main(String[] args){
                 int i;
                 i = (int)'a'; // type case operator
    }
}
10. Assignment operator: This is for assigning a value to an variable
   Example:

      int i;
      i = 10;

11.   Compound assignment operator : This is apply on single operand
Example:

   public class Shift_Operators {
       public static void main(String[] args){
                 int i=10;
              System.out.println(i+=1);// this is equal i=i+1
              System.out.println(i-=1);// this is equal i=i-1
              System.out.println(i*=1);// this is equal i=i*1
              System.out.println(i/=1);// this is equal i=i/1
             
       }
}

Output:
11
10
10
10

12.Conditional operator: This is for condition checking.
    

        Syntax:
          {datatype} Identifier = (Boolean expression)? value1: value2;
             If Boolean expression is true then value1 will assign to identifier else value2 will assign to identifier.

Example:

   public class Conditional_Operator {
       public static void main(String[] args){
                 int i=10;
              String value;
              value = (i>1)?"i greatherthan 10":"i lessthan 10";
              System.out.println(value);
             
       }
}

Output:

     i greatherthan 10

No comments:

Post a Comment