Operators in Java

Sunday, February 05, 2017 Unknown 0 Comments




Operator is a symbol to perform a certain operation on given operands.

Type of operators

1 . Arithmetic operators

2 . Relational operators

3 . Bitwise operators

4 . Logical operators

5 . Assignment operators


Now we will go through each kind of operator one by one


Arithmetic operator : 

These operators are used in mathematical expressions in the same way that they are used in algebra.
Assume integer variables X and Y having values 10 and 5 respectively.

   + (Addition) : Adds values on either side of operator.  
                                X+Y=10+5 gives 15 as result.

    - (Subtraction) : Subtracts second value from the first one.
                              X-Y =10-5 gives 5 as result.

   * (Multiplication) :  Multiplies values given on either side.
                                X*Y=10*5 gives 50 as result.

   / (Division) : Divides first value by second one.
                       X/Y=10/5 gives 2 as result.  

  % (modulus) : Gives remainder if, a value is divided by another.
                          X%Y=10%5 gives 0 as result.

  ++ (Increment operator) : Increases a value by 1.
                                        X++ is equivalent to x+1 i.e. 10+1 gives 11 as 
                                         result.

  -- (Decrement operator) : Decreases a value by 1.
                                         X-- is equivalent to X-1 i.e. 10-1 gives 9 as result. 

Relational Operators : 

 Relational operators are used to compare operands.

Assume two integers X and Y having 10 and 5 respectively.

There are following type of relational operator.

==(equal to) :  Checks if the values of two operands are equal or not, if yes  
                          then condition becomes true.
                         (X==Y) is false.

!= (not equal to) : Checks if the values of two operands are equal or not, if 
                              values are not equal then condition becomes true.
                             (X!=Y) is 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) is true.

< (less than) :  Checks if the value of left operand is smaller than the value 
                        of  right operand, if yes then condition becomes true.
                        (X<Y) is false.

>= (greater than or equal to) :  Checks if the value of left operand is 
                                                  greater or equal to value of the left operand.
                                                  (X>=Y) is true.

<= (less than or equal to) :   Checks if the value of left operand is smaller
                                                   or equal to value of the left operand.
                                                  (X<=Y) is false.  

Bitwise operator :

Java Bitwise operators can be applied to integer types, long, int, short, char and byte.
Bit wise operator works on bits.
Assume int X=10 and Y=5 and their bit conversions are as follows
X=00001010
Y=00000101

 & (Bitwise AND) : Binary AND operator copies a bit to the result if it exists  
                              in both operands.
                             X&Y=00000000.

| ( bitwise OR) :    Binary OR operator copies a bit if it exists in either 
                               operand.
                               X | Y = 00001111.

^ (Bitwise XOR) : Binary XOR operator copies bit if it is set in one operand                                    but not both.
                              X ^ Y = 00001111.

~ (Bitwise complement) : It is used to determine the negation of given 
                                       value.It is 2's complement of the given value
                                       ~X = 11110110

<< (Left shift operator) : The left operand value is moved left by the  
                                       number of bits specified by right operand.
                                       X<<2 = 10<<2 = 00101000

>> ( Right shift operator) : The left operand value is moved right by 
                                             the number of bits specified by right operand.  
                                            X>>2 = 10>>2 = 10000010


Logical Operator :

This operator returns result in boolean(true or false).
Assume there are two boolean variables X and Y having values true and false respectively.

&& (Logical AND) : If both operands are true then (X&&Y) becomes true 
                             otherwise becomes false.

|| (Logical OR) : If any one of the operands has value true (X||Y) becomes 
                          true.

! (Logical NOT) : Reverse the logical state of the operand (!X) is false (!Y) is 
                          true 

Assignment Operator : 
           
This operator is used to assign a value to a variable.
e.g. int x=5; 


Previous topic                                                                     Next topic
Unicode system                                                                  Ternary operator

            
                                     



     

0 comments: