Saturday 21 January 2017

Operators in Java


                                              
operator in java


Java provides a rich operator environment. Most of its operators can be divided into 4 groups : Arithmetic , Logical  , Relational , Bitwise.


Arithmetic Operator

Arithmetic operators are used in mathematical expression in same way that they are used algebra. Various arithmetic operator are :

                     Operator                                Result

                         +                                           Addition
                         -                                      Subtraction ( Unary Minus)
                        *                                            Multiplication
                        /                                             Division
                        %                                           Modulus
                       ++                                           Increment
                        --                                            Decrement
                       +=                                           Addition assignment
                       -=                                            Subtraction assignment
                       *=                                           Multiplication assign.
                       /=                                            Division assignment
                       %=                                          Modulus assignment

The operands of the arithmetic operators must be of a numeric type.We cannot use them on boolean types, but can be used on char types.

Bitwise Operators

Java defines several bitwise operators which can be applied to the integer types long , int , short  , char and byte. These operators act on the individual bits of their operands. Some of operators are:

                        Operator                             Result
           
                            ~                                         Bitwise unary NOT
                            &                                        Bitwise AND
                            |                                           Bitwise OR
                            ^                                         Bitwise exclusive OR
                            >>                                       Shift right
                           >>>                                      Shift right Zero fill
                            <<                                       Shift left
                           &=                                       Bitwise AND assign.
                           |=                                         Bitwise OR assignment
                           ^=                              Bitwise exclusive OR assign.
                          >>=                               Shift right assignment
                          >>>=                          shift right zero fill assign.
                          <<=                              shift left assignment

Relational Operator

The Relational Operators determine the relationship that one operand has to the other. They determine Equality and Ordering.

                     Operator                              Result 

                       ==                                         Equal To
                       !=                                          Not equal to
                      >                                            Greater Than
                      <                                            Less than
                       >=                                        Greater than or equal to
                      <=                                         Less than or equal to

The outcome of these operations is a boolean value. The relational Operators are used more frequently in the expression that control the if statement and looping statements.

Logical Operators

The Logical Operators shown here operate only on boolean operands. All logical operators combine 2 boolean values and form a resultant boolean value.

              Operator                                       Result

                  &                                                 Logical AND
                   |                                                  Logical OR
                   ^                                                 Logical XOR
                  ||                                           Short circuit OR
                  &&                                      Short circuit AND
                  !                                           Logical Unary NOT
                  &=                                           AND assignment
                  |=                                                OR assignment
                 ^=                                              XOR assignment
                 ==                                                Equal to 
                  !=                                                Not equal to
                 ?:                                                Ternary If-Then-Else

The logical operator & , | , ^ operate on boolean values in the same way that they operate on the bits of an integer.


Assignment Operator

the assignment operator is the single equal sign, = . The general form for the same is : 
      
                          var=expression;
  Here , the type of var must be compatible with the type of Expression.

It also allows us to create a chain of assignment.
  
        Ex- int x, y ,z;
                x=y=z=100;

Ternary Operator

 The ternary operator allows you to replace certain types of if-then-else statements. The general from of this is:

   expression 1 ? expression 2 : expression 3

Here expression 1 can be any expression that evaluates to a boolean value. If expression 1 is true , then expression 2 is evaluated ; otherwise , expression 3 is evaluated.

            
                                                                By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *