Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Thursday, 21 July 2011

Operators in Java

     An operator is a symbol which represents some operation that can be performed on data. There are eight operators available in java to carry out the operations. They are
1.       Arithmetic operators
2.       Relational operators
3.       Logical operators 
4.       Short hand assignment operators
5.       Increment and decrement operators
6.       Conditional operators
7.       Bitwise operators
8.       Special operators


ARITHMETIC OPERATORS

Arithmetic operators are used to do arithmetic calculations. There are two types
1.       Binary operators
2.       Unary operators

Binary operators

     Binary operators need two operands for operations. They are

Operator
Operation
Example
+
-
*
/
%(modulo operator)
Addition
Subtraction
Multiplication
Division
Remainder after integer division
X=5+3
Y=x-10
Z=5*3
X=5/3=1 or 1.67
X=5%3=2

Unary operators

    Unary operators need only one operand for operation. They are

Operator
Operation
Example
-
++
--
Unary minus
Increment
Decrement
-10
++i
--I
 


 RELATIONAL OPERATORS

     Relational operators are used to find out the relationship between two operands. They are

Operator
Operation
Example
>=
<=
==
!=
Greater than
Less than
Greater than equal to
Less than equal to
Equal to
Not equal to
A>B
A<B
A>=B
A<=B
A==B
A!=B

LOGICAL OPERATORS

     Logical Operators are used to find out the relationship between two or more relational expressions. They are

Operator
Meaning
&&
||
!
AND
OR
NOT

     Logical operators return results as indicated in the following table.

x
y
x&&y
X||y
T
T
F
F
T
F
T
F
T
F
F
F
T
T
T
F

INCREMENT AND DECREMENT OPERATORS
    
     There are two special operators in java to control the loops in an effective manner. These operator are called increment and decrement operators.

Increment operator

     ++ is increment operator. This adds 1 to the value contained in the variable. The general form is

     Variable++ OR ++Variable
     
     The first form (post fix) uses the variable and increments afterwards. The second form (prefix) increments the variable and uses it.

Example
      
     a++ means a=a+1
     ++a means a=a+1

Decrement operator

     -- is the decrement operator. This subtracts a from the value contained in the variable. The general form is

     Variable-- OR –Variable

    The first form (postfix) uses the variable and decrements afterwards. The second form (prefix) decrements the variable and then uses it.

Example
     
     --a means a=a-1
     a-- means a=a-1

SHORT - HAND ASSIGNMENT OPERATORS

     Short – hand assignment operators are operations which are used to simplify the coding of certain type of assignment statement. The general form is

     Variable operator=expression

Example
The table given below lists the operators with example.

Operator
Operation
Example
+=



-=



*=




/=




%=
Value if LHS variable will be added to the RHS value add is assigned to LHS variable.
RHS value will be subtracted from LHS variable and is assigned to LHS variable.
Value of lhs variable will be multiplied by the RHS value and is assigned to LHS variable.
Value of LHS variable will be divided by the RHS value and is assigned to LHS variable.
Value of LHS variable will be divided by RHS value and the remainder will be stored in the LHS variable.
X=x+10 can be written as x+=10


X=x-10 can be written as x-=10


x=x*10 can be written as x*=10



x=x/10 can be written as x/=10



x=x%10 can be written as x%=10


CONDITIONAL OPERATOR
  
     The conditional operators ? and : are used to build simple conditional expression. It has three operands. So it is called ternary operator. The general form is

      Expression1?expression2:expression3;

     Expresion1 is evaluated first. If it is true expression2 is evaluated. If expression1 is false expression3 is evaluated.

Example

     Big=a>b?a:b;

     In this condition a>b is tested first. If this is true big=a else big=b.

BITWISE OPERATOR

     Bitwise operators are used to do bit by bit operation. The table given below lists the operators and its meaning.


Operator
Meaning
&
|
^
>> 
<< 
~
>>> 
Bitwise AND
Bitwise OR
Bitwise EX – OR
Bitwise right shift
Bitwise left shift
Bitwise complement
Shift right with zero fill

SPECIAL OPERATOR

     There are two important special operators. They are
1.       instanceof operator
2.       dot operator (.)

     instanceof operator is used to find out whether the given object belongs to a particular class or not. The general form is
   
      objectname instanceof classname

it gives a true value if the object on the left hand side of the operator belongs to the class on the right hand side else false.

Example

     Ramu instanceof sport
This statement gives a true value if the object ramu belongs to the class sport else false.

     Dot operator (.) is used to access the variables and methods of class objects. The general form is

     Objectname.variable or method

Example

     Ramu.group;
     Ramu.add();

No comments:

Post a Comment

Followers