Sunday, 8 June 2014

Operators in JAVA

OPERATORS 


Operators are the keywords in any programming language that perform the specific pre defined or user defined task on a set of operands . Operands are the values on which the operation is performed to result into a specific value . 
Following types of operators are available in  JAVA . Here is the categorization of Operators and their details with examples :

Arithmetic operators :
  • Arithmetic operators as the name suggests perform the arithmetic operations .
  • Arithmetic operations are performed on two given operations .
  • Following Arithmetic operations are available in JAVA  :
  1. Addition (+)
  2. Subtraction(-)
  3. Multiplication(*)
  4. Division(/)
  5. Modulus (%)
Here is an example of modulus operator :
25%5=5
i.e. modulus operator between two operands gives us the result and would be equal to the remainder that we get after dividing both the operands.

Now, how  you think a java program will resolve the following problem:
  1. (-10)%(3)
  2. (10)%(-3)
  3. (-10)%(-3)  
These are three different cases and it should be made clear clear that SIGN OF DENOMINTOR IS NOT CONSIDERED , IT ASSIGNES THE SIGN OF NUMERATOR AS THE SIGN OF FINAL RESULT.

Hence result to above three problems would be :

  1. (-10)%(3)     = -1
  2. (10)%(-3)     =   1
  3. (-10)%(-3)    = -1
Logical Operators

These operators perform the logical tasks . We have three logical operatos and these are :
  1. AND operator (&&)
  2. OR operator (||)
  3. NOT operator (!)

AND operator considers the statement to be valid if both the two statements on which operator is applied are true . In other words we can say that , logic is true iff both A and B are true.

OR operator considers the statement to be valid if any one statement among the given is valid .
In other words logic is true if any one operand is true .

NOT operator compliments the logic . That is it changes true to false and vice versa.

for more clarification consider the table below :

     A
     B
A&&B
  A||B
      0
      0
      0
      0
      0
      1
      0
       1
      1
      0
      0
       1
      1
      1
      1
        1


Conditional operators :We have only one conditional operator called  TERNARY operators.
the operator is represented by  ?:  and it works like an if else statement .
consider the following statement 
                                                           
a>b?a:b

It means that "If a>b then a Else b" 


Relational Operators :


Relational operators are basically used to compare two numbers. 
in JAVA we have the following relational operators . 


( > , < , >= , <= , == )  and these are (greater than , less than , greater than equl to , less than equal to , to check the equality) respectively .

Bit -Wise Operators :

Bit Wise operators are  (&,|, ^, >> ,<<).

Now the point is that what is the difference between logical and Bit-Wise operators ?

Both of the operators perform AND , OR operation but the difference is in the way they perform .
in BIT-WISE operators the operands are first converted into the binary form  i.e.  bits and then the operation is performed .



Increment and Decrement Operators :

These operators are called UNARY OPERATORS as they operate on a single Operand.
Increment operator increments the number and operates in two ways : 

++a  and  a++  :


a++
(Post increment)
++a
(Pre Increment)
Consider the following expression :
 int a = 10 ;
int b = a++; 
It will first assign the value of ‘a’ to ‘b’ and then will increment ‘a’.


The output of the above expression would be :

a = 11
b = 10

Consider the following expression :
 int a = 10 ;
int b = ++a ;

It will first increment the value of ‘a’ and then will assign the value to ‘b’.

The output of the above expression would be :

a = 11
b = 11



                               


Same is in the case of decrement operator . It decreases the value of operand by 1 . 
In decrement operator also we have same same post decrement ans pre decrement and they work in similar way :


a--
(Post increment)
--a
(Pre Increment)
Consider the following expression :

int a = 10 ;
int b = b-- ;

It will first assign the value of ‘a’ to ‘b’ and then will decrement ‘a’.


The output of the above expression would be :

a = 9
b = 10

Consider the following expression :

int a = 10 ;
int b = --b ;

It will first decrement the value of ‘a’ and then will assign the value to ‘b’.


The output of the above expression would be :

a = 9
b = 9



                                

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes