Wednesday 21 May 2014

Operators In Java

Java provides a rich set of operators to manipulate variables.Most of its operator can be divided into the following group.










1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Bitwise Operators
5. Logical Operators
6. Other Operators
.

 The Arithmetic Operators and Assignment Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
Assume integer variable X holds 40 and variable Y holds 20, then:


Operator Description Result
+ Addition - Adds values on either side of the operator X + Y gives 60
- Subtraction - Subtracts right hand operand from left hand operand X - Y gives 20
* Multiplication - Multiplies values on either side of the operator X * Y gives 800
/ Division - Divides left hand operand by right hand operand X / Y gives 2
% Modulus - Divides left hand operand by right hand operand and returns remainder X % Y gives 0
++ Increment - Increases the value of operand by 1 X++ gives 41
-- Decrement - Decrement the value of operand by 1 X-- gives 39
+= Addition assignment - Addition assignment add right side operand into left operand and assign to the left operand X += 10 is same as X=X+10
-= Subtraction assignment - Subtraction assignment subtract right side operand from left operand and assign to the left operand X -= 10 is same as X=X-10
*= Multiplication assignment - Multiplication assignment multiply right side operand with left operand and assign to the left operand X *= 10 is same as X=X*10
/= Division assignment - Division assignment divide left side operand by right operand and assign to the left operand X /= 10 is same as X=X/10
%= Modulus assignment - Modulus assignment Divides left hand operand by right hand operand and assign to the left operand X %= 10 is same as X=X%10
package org.modi;

public class Operator
{
 
 public static void main(String[] args)
 {
  int x=40;
  int y=20;
  System.out.println("Addtion : "+(x+y));
  System.out.println("Subtraction : " +(x-y));
  System.out.println("Multiplication : "+x*y);
  System.out.println("Division : "+x/y);
  System.out.println("Modulus : "+x%y);
  
 }

}
output:
Addtion : 60
Subtraction : 20
Multiplication : 800
Division : 2
Modulus : 0
The Relational Operators:
The Relational Operators determine the relationship that one operand has to other .
The output of these operations is boolean value.The relational operators are most used in the expression that control the if and various loop statement.
Assume integer variable X holds 40 and variable Y holds 20, then:


Operator Description Result
== Equals to - Checks if the values of two operands are equal or not, if yes then return true X==Y returns false
!= Not equals to - Checks if the values of two operands are equal or not, if values are not equal then returns true. X!=Y returns true
> Greater than Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. X > Y returns true
< Less than-Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. X < Y returns false
>= Greater than or equal to-Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. X >= Y returns true
<= Less than or equal to-Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. X <= Y returns false
package org.modi;

public class RelationalOperator
{
 
 public static void main(String[] args)
 {
  int x=40;
  int y=20;
  System.out.println("X==Y : "+(x==y));
  System.out.println("X!=Y :"+(x!=y));
  System.out.println("X>Y : "+(x>y));
  System.out.println("X>=Y : "+(x>=y));
  System.out.println("X<=Y : "+(x<=y));
  
 }

}
output:
X==Y : false
X!=Y :true
X>Y : true
XX>=Y : true
X<=Y : false
The Bitwise Operators:
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.These operator acts upon individual bits of their operands.
Assume integer variable X holds 40 and variable Y holds 20, then binary format is
X=0010 1000 , Y=0001 0100

Operator Description Result
& Binary AND Operator copies a bit to the result if it exists in both operands. X&Y returns 0000 0000 means 0
| Binary OR Operator copies a bit if it exists in either operand. X|Y returns 0011 1100 means 60
^ Binary XOR Operator copies the bit if it is set in one operand but not both. X^Y returns 0011 1100 means 60
~ Unary bitwise complement. ~X returns -41
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. (X<< 2)  returns 160
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. X>>2 returns 10



The Logical Operators:
Assume Boolean variables X holds true and variable Y holds false, then:
Operator Description Result
&& Logical AND operator X&&Y returns false
|| Logical OR operator X||Y returns true
! Logical NOT operator !X returns false


Other Operator:
Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
public class Test {

   public static void main(String args[]){
      int a , b;
      a = 10;
      b = (a == 1) ? 20: 30;
      System.out.println( "Value of b is : " +  b );

      b = (a == 10) ? 20: 30;
      System.out.println( "Value of b is : " + b );
   }
}
output:
Value of b is : 30
Value of b is : 20

0 comments :

Post a Comment