Thursday, February 23, 2017

Operators in Java

Operators in Java


Basically we can treat operators in three category on the basis of operand used with the operators i.e
1> Unary Operator
2> Binary Operator
3> Ternary Operator

Unary Operator 

It is the operator used with the single operand . As its name indicate " unary " that means " single ".
Some of the unary operators are increments ( ++ ) and decrements ( -- ) operators. We will study in details about these operators in their respective topic later in this tutorial.

We can also use " + " operator as unary operator which is in java formally called as " string concatenation operator ".
for ex-

int   x  =  + 10 ; // Here x represents +ve 10 value but itself by default it is +ve. 

byte   b =  +x  ; // Invalid. Here it will give compilation error because we are storing int                                    value " x " in byte type"  b " so be careful by assigning any variable to
                             another variable.


int   x  =  -10 ;     // valid statement represent -10. 

byte   b  =  -x;     // invalid same reason as above.

int    x  =  ++10 ; // Here ++ represents the increments operator .

int    x  =  --10 ;   // Here -- represents the decrements operator.


Binary Operator 

It is the operator used with two operands and we all know that binary represent two. So we need to operate with this kind of operator.
For example we are using following operator as an binary operator.

String concatenation operator ( + ), Assignment Operator ( = ), Relational Operator ( Both equality and comparison operators ) , Logical operators , Bitwise operators  and shift operators. 

int    x  =  10 + 20 ; // Here" + " represents the use of Binary operator .

int    x = 20 ; // Here " = " is an Assignment operator used left side as an variable and                        
                         right side is a value of it.


if ( 10 < 20 ) // Here " < " is an Comparison operator , which returns true or false and it                  
                        must have 
value as an operand not variable as an operand .  

if ( 10 >= 20 ) // Here " >= " is also an Comparison operator , which returns true or false                
                         and it must have value as an operand 
not variable as an operand .

if ( 10 == 10) //Here " == " is also an Equality operator , which returns true or false and it                
                        must have 
as an operand not variable as an operand . 

if ( 10 != 10) //Here " != " is also an Equality operator , which returns true or false and it                  
                        must have value as an operand not variable as an operand . 


Ternary Operators 

It a kind of operator which is used with three operands for example conditional operator is one of the ternary operator.

Syntax :-
<operand 1 >  ?  <operand 2 >   :  <operand 3 >
Here the condition is < operand 1 > must return boolean value i.e true or false .
If < operand 1> returns true then < operand 2> will be displayed as an output and if false then < operand 3> will be displayed as an output.

for ex-
int   x  =  ( 20 > 10 )  ?  20  : 10 ; // here condition is true that means 20 is displayed as an output.

Available link for download