Java Operators

                                   Java Operators

Java provides richer operator environment. It has following types.
  1. Arithmetic Operators:

  2. Bitwise Operators:

  3. Relational Operators:

  4. Boolean Logical Operators:

  5. Assignment Operators:

  6. ? Operators:

Arithmetic Operators:

 Arithmetic operators are used in mathematical expression in the same way that they are use in algebra. The following table list the arithmetic operators.

Operator              Result

      +                    Addition
      -                     Subtraction(also unary minus)
      *                    Multiplication
      /                     Division
     %                   Modulus
     ++                   Increment
     +=                  Addition Assignment
     -=                   Subtraction Assignment   
     *=                   Multiplication Assignment
     /=                    Division Assignment
     %=                 Modulus Assignment
     --                     Decrement

Bitwise Operators:

Operator              Result

      ~                    Bitwise unary NOT
      &                   Bitwise AND
       |                    Bitwise OR
      ^                    Bitwise exclusive OR
      >>                  Shift Right
     >>>                Shift Right Zero Fill
      <<                  Shift Left
      &=                 Bitwise AND Assignment
       |=                  Bitwise OR Assignment
       ^=                 Bitwise exclusive OR Assignment
      >>=                Shift Right Assignment
     >>>=               Shift Left Assignment

Relational Operators:

Operator               Result

     ==                    Equal to
     !=                     Not Equal to
     >                       Greater then
     <                       Less then
     >=                     Greater then or Equal to
     <=                     Less then or Equal to

Boolean Logical Operators:

Operator                Result

      &                     Logical AND
       |                      Logical OR
      ^                      Logical XOR(exclusive OR)
      ||                      Short-circuit OR
     &&                   Short-circuit AND
      !                       Logical unary NOT
     &=                    AND Assignment
      |=                     OR Assignment
      ^=                    XOR Assignment
      ==                    Equal to
      !=                     Not Equal to
      ?:                      Ternary if-then-else

Assignment Operators:

The assignment operator is the single equal sign =.
var = expression;
Here, the type of var must be compatible with the type of expression.
exp:
int x, y , z;
x = y - z = 100; // set x, y and z to 100

? Operators:

Java includes a special ternary operator that can replace certain types of of-then-else statements. This operator is the ?.
expression1 ? expression2 : expression3
Here, expression1 can be any expression that evaluates to a boolean value . If expression1 is true, then expression2 is evluated; otherwise, expression3 is evaluated. The result of the ? operation is that of the expression evaluated. Both expression2 and esxpresion3 are required to return the same type, which can't be void.
exp:
ratio = denom == 0 ? 0 : num / denom;
It first looks at the expression to the left of ? . If denom equal zero, then expression between the ? and the : is evaluated and used as the value of the entire ? expression. If denom is not equal to zero, then the expression after the : is evaluated and used for value of the entire ? expression. The result produced  by the ? operator is then assigned to ratio

Written By: Asad Hussain

Post a Comment

Previous Post Next Post