Monday, 24 March 2014

OPERATOR IN JAVA

Posted By: Unknown - Monday, March 24, 2014


Java have very rich set of operators in Java. Once we know about data types and variabes then it is good to know operators in java. Operator in Java can be majorly categorized into following :
1. Arithmetic Operators :
Arthmetic operators are used in mathmatical calculation and algebra. It is also used same way in our real life and school books.
List of arthmetics operators are given below along with examples :
i) + Plus Operator – It adds two or more numbers. Examples: 2 + 5 Result: 7, 2 + 3 + 5 Results: 10
ii) – Minus (Subtract) operator : It subtract one number from other. Examples : 5 -3 Results: 2
ii) * (Multiply) Operator : This operator is used to multiply two numbers. Examples : 2 * 5 Result : 10
iv) / ( Divide ) Operator : This is operator is used to divide two numbers. Examples : 10 /2 Result : 5
Example :
class ArthimaticEx
{
  public static void main(String args[])
  {
    int num1 = 10, num2 = 5;
    int Result = num1 + num2;
    System.out.println("Addition of "+num1+" and "+num2+" is equal to "+Result );
    Result = num1 - num2;
    System.out.println("Substract of "+num1+" and "+num2+" is equal to "+Result );
    Result = num1 * num2;
    System.out.println("Multiply of "+num1+" and "+num2+" is equal to "+Result );
    Result = num1 / num2;
    System.out.println("Division of "+num1+" and "+num2+" is equal to "+Result );
  }
}
Output of the Arithmetic Operator Example :
Addition of 10 and 5 is equal to 15
Substract of 10 and 5 is equal to 5
Multiply of 10 and 5 is equal to 50
Division of 10 and 5 is equal to 2
2. Relational Operators
Relational Operators is used in comparative statement in Java. Following list of relational operator in java i) == Equal Operator : Checks values for equal between two operands and return true if both are same otherwise false ii) != Not equal Operator : Checks values for not equal between two operands and return true if both are not same otherwise false iii) > Greater Than Operator : Checks values for e between two operands and return true if first operand is greater than second otherwise false vi) < Smaller Than Operator : Checks If first operand is small than second then return true otherwise false v) >= Greater and Equal than Operator : Checks value if first operands is greater than or equal to second then return true otherwise false vi) <= Less than and Equal Operator : Checks value if first operator is less than or equal to second then return true otherwise false
class RelationalOperatorEx
{
 public static void main(String args[])
 {
 int num1 = Integer.parseInt(args[0]);
 int num2 = Integer.parseInt(args[1]);

 if( num1 == num2 )
  System.out.println("Both numbers are equal");
 else
  System.out.println("Both numbers are not equal");

 if( num1 != num2 )
  System.out.println("Both numbers are not equal");
 else
  System.out.println("Both numbers are equal");

 if( num1 > num2 )
  System.out.println("First operand is greater than second operand");
 else
  System.out.println("First operator is equal or less than second operand");

 if( num1 < num2 )   
          System.out.println("First operand is less than second operand");  
        else
   System.out.println("First operator is equal or greater than second operand");  
        if( num1 >= num2 )
  System.out.println("First operand is greater or equal than second operand");
 else
  System.out.println("First operator is less than second operand");

 if( num1 <= num2 )
      System.out.println("First operand is less or equal than second operand");
 else
      System.out.println("First operator is greater than second operand");
 }
}
Output of Relational Operator Example :
C:\>java RelationalOperatorEx 10 20
Both numbers are not equal
Both numbers are not equal
First operator is equal or less than second operand
First operand is less than second operand
First operator is less than second operand
First operand is less or equal than second operand
3. Logical Operators These operators are used with boolean values i.e. True and False. If we have multiple condition which return boolean value and we want to take some decision on based of all conditions then we can use logical operators.
Following are list of logical operators in Java : i)  &&  AND Operator: For example, we want to do some action based on two condition (x<y), (y<z) and it means that if both condition are true then do some action. This can be done in just single line. If this operator was not available then we have to use multiple lines.
Both condition’s output should be true.
If both condition are true and && operation is used then Result : True
Conditions Results ======================================= True && True True True && False False False && True False False && False False
class ANDOperatorEx
{
 public static void main(String args[])
 {
 boolean bool1 = true;
 boolean bool2 = false;

    if (bool1 && bool2)
  System.out.println("This Will not printed as explained");

  if (bool2 && bool1)
  System.out.println("This Will not printed as explained");

 bool1 = true;
 bool2 = true;

  if (bool2 && bool1)
  System.out.println("This Will print as explained");

 bool1 = false;
 bool2 = false;

  if (bool2 && bool1)
  System.out.println("This Will not be printed");
 }
}
Output of AND Operator example :
  This Will print as explained
ii) || OR operator : As name suggest, if any of the condition output is true in both condition then result will be true.
Conditions Results ======================================= True || True True True || False True False || True True False || False False
class OROperatorEx
{
 public static void main(String args[])
 {
 boolean bool1 = true;
 boolean bool2 = false;

    if (bool1 || bool2)
  System.out.println("This Will print as explained");

  if (bool2 || bool1)
  System.out.println("This Will print as explained");

 bool1 = true;
 bool2 = true;

  if (bool2 || bool1)
  System.out.println("This Will print as explained");

 bool1 = false;
 bool2 = false;

  if (bool2 || bool1)
  System.out.println("This Will not be printed");
 }
}
Output of OR operator example is given below :
C:\>java OROperatorEx
This Will print as explained
This Will print as explained
This Will print as explained
iii) ! NOT Operator : It changes True to False and False to True :
Conditions Results ======================================= !True False !False True
class NotOperatorEx { public static void main(String args[]) { boolean bool1 = true; boolean bool2 = false;
if (!bool1) System.out.println(“This should not print since condition will be false”); else System.out.println(“Condition is false so else should print”);
if (!bool2) System.out.println(“Boolean value will be true and it will print this line”); else System.out.println(“if condition is true so this will not print”); } } Output of  Not Operator Example :
C:\>java NotOperatorEx
Condition is false so else should print
Bollean value will be true and it will print this line
4. Bitwise and Bit Shift Operators :  Java program language provides feature to work on bit by bit.  It is rarely used feature.  It is important to know since it provides speed boost.  It works only integer values.
I) Bitwise & Operator : It works like & operator.  If both operand have 1 bit in their binary equivalent then return will be 1 otherwise it will be 0.
First Operand Binary Digit                   Second Operand Binary Digit                          Result 1                                                                        1                                                                         1
1                                                                        0                                                                         0
0                                                                        1                                                                         0
0                                                                        0                                                                         0
Examples :
14 ( 1110  0000) && 60 ( 1111 0000)   results : 12
II) Bitwise I  Operator : It works like | operator.  If both operand have 0 bit in their binary equivalent then return will be 0 otherwise it will be 1.
First Operand Binary Digit                   Second Operand Binary Digit                          Result
1                                                                        1                                                                         1
1                                                                        0                                                                         1
0                                                                        1                                                                         1
0                                                                        0                                                                         0
Example :
14 ( 1110  0000) | 60 ( 1111 0000)   results : 62
III) Bitwise ~ Operator : It works only in single value.  It changes in binary digit 0 to 1 and 1 to 0.
Operand Binary Digit                 Result
1                                                      1
0                                                      1
Example :
~ 14 Results -15
IV) Bitwise << Left Shift Operator : It shifts left side all values and fill 0 in last place.  Num << 1 will shift all bit to one place in left side.  
14 << 1 Results 28
 V) Bitwise >> Right Shift Operator : It shifts left side all values and fill 0 in last place.  Num << 1 will shift all bit to one place in right.
14 >> 1 Results 0  
VI) Bitwise >>> Shift O Fill Operator : It will shift to left based on value on right operands and fill those gaps with 0.
14 >>> 1 Results 7
Example of Bitwise Operator :
class BitWiseOperatorExample
{
public static void main(String args[])
{
int num1 = 22;
int num2 = 12;

// Bitwise & operator
int result = num1 & num2;
System.out.println(num1 + " & "+ num2 + " Results " + result );

// Bitwise | operator
result = num1 | num2;
System.out.println(num1 + " | "+ num2 + " Results " + result );

// Bitwise ~ operator
result = ~num1;
System.out.println(" ~ "+ num1 + " Results " + result );

// Bitwise << operator
result = num1 << 1;
System.out.println(num1 + " << 1"+ " Results " + result );

// Bitwise >> operator
result = num1 >> num2;
System.out.println(num1 + " >> 1" + " Results " + result );

// Bitwise >>> operator
result = num1 >>> 1;
System.out.println(num1 + " >>> 1" + " Results " + result );
}
}
Output of bitwise operator example :
G:\>java BitWiseOperatorExample
22 & 12 Results 4
22 | 12 Results 30
~ 22 Results -23
22 << 1 Results 44 22 >> 1 Results 0
22 >>> 1 Results 11
5. Assignment Operators :
java have many assignment operators.  Some assignment operator like += ..etc provides speed boost.
List of assignment operators in java given below with examples at last :
i)   = operator : It is simple assign operation like a=12;.  12 value will be assignment to variable a.
ii)   += operator :  a += 12; This means  a = a+12;
iii)   -= operator :  a -= 12; This means  a = a-12;
vi)   *= operator :  a *= 12; This means  a = a*12;
v)   /= operator :  a /= 12; This means  a = a/12;
Examples of Assignment operators  in java:
class AssignmentOperatorExample
{

  public static void main(String args[])
  {
     int num1 = 10; 
     int num2 = 20;
     int result = 0;

     result = num1 + num2;
     System.out.println("result = "+num1+" + " + num2+"  = " + result );

     result += num1 ;
     System.out.println("result += num1  = " + result );

     result -= num1 ;
     System.out.println("result -= num1 = " + result );

     result *= num1 ;
     System.out.println("result *= num1 = " + result );

     result /= num1 ;
     System.out.println("result /= num1 = " + result );
    }
}
Output of assignment operator examples:
G:\>java  AssignmentOperatorExample
result = 10 + 20  = 30
result += num1  = 40
result -= num1 = 30
result *= num1 = 300
result /= num1 = 30
7. Modulus operator : It provides us remaining value. Examples : 30 % 10 Result : 0, 20 % 3 Result : 2 When we divide 30 with 10 (30/10) then we get remaining value after divide 0. When we divide 20 with 3 then we get remaining value 2 ( 20-18).
Example of Modulus Operator in Java :
class ModulusOperatorExample
{
  public static void main(String args[])
  {
 System.out.println("10%3 = "+10%3);
 System.out.println("10%5 = "+10%5);
 System.out.println("20%3 = "+20%3);
    }
}
Output of Modulus operator :
G:\>java ModulusOperatorExample
10%3 = 1
10%5 = 0
20%3 = 2
8. Increment and Decrement Operator :
Increment operator provides speed boost. We have ++, — operator. We also need to consider prefix and suffix on same. ++ operator will increase value by 1 and — decrements operator will decrease by 1. Examples are given below : I) X++ Increment Example:
class IncrementSuffix
        {
   public static void main(String args[])
   {
     int num =12;
     System.out.println(num++);
     System.out.println(num);
   }
        }
Result:
  12
  13
Explanation : System.out.println will use previous value to print first and then increments takes place. When we print again then we get incremented value.
II) X– Decrements Example:
class DecrementSuffix
        {
   public static void main(String args[])
   {
     int num =12;
     System.out.println(num--);
     System.out.println(num);
   }
        }
Result:
  12
  11
Explanation : System.out.println will use previous value to print first and then decrements takes place. When we print again then we get decremented value.
III) ++X Increment Example:
class IncrementPrefix
        {
   public static void main(String args[])
   {
     int num =12;
     System.out.println(++num);
     System.out.println(num);
   }
        }
Result:
  13
  13
Explanation : System.out.println will increment first and then print value on first statement and second statement.
VI) –X Decrements Operator Example:
class DecrementPrefix
        {
   public static void main(String args[])
   {
     int num =12;
     System.out.println(num--);
     System.out.println(num);
   }
        }
Result:
  11
  11
Explanation : System.out.println will decrement first and then print value on first statement and second statement..
6. Other Operators
We have only two operator left to discuss i.e. Ternary Operator and InstanceOf operator I) Ternary Operator : It is also called short hand of if then else condition. Expression : (a<b) ? a : b Explanation of this expression : if condition is true then answer will be a else it will be b.
Example of Ternary operator :
class TernaryOperatorExample
{
  public static void main(String args[])
  {
  int num1 = 12;
  int num2 = 14;
  int result = (num1 < num2) ? num1 : num2;

       System.out.println("Result = "+result);

    if (num1<num2)
       System.out.println("Result = "+num1);
    else
       System.out.println("Result = "+num2);

    }
}
Output of example :
G:\>java TernaryOperatorExample
Result = 12
Result = 12
II) Instanceof Operator : It checks instance of object belongs to which class.  If obj is instance (object) of Class Test and we ask obj instanceof Test then answer will be true.  This is really useful when we create object by parent class or interface to any class at run time.
Sample Example of InstanceOf operator in Java :
class Test
{
 public static void main(String args[])
 {
  Test obj = new Test();
  System.out.println(obj instanceof Test);
 }
}
Output of instanceof operator example :
true

0 comments:

Post a Comment

this blog is helpful or not

International

Auto News

Translate

Pages

Popular Posts

Popular Posts

Designed By Templatezy / Sb Game Hacker Apk / MyBloggerThemes