Pages

 

Java Fundamentals part-3

Operaters: 

An operator is a special symbol that operates on data.

Operators are divided into many categories based on their operations:

    1)    Arithmetic Operators

    2)    Relational Operators

    3)    Logical Operators

    4)    Increment & Decrement Operators

    5)    Bitwise Operators

    6)    Assignment Operators

    7)    Conditional Operators

    8)    Unary Plus & Unary Minus Operators

    9)    Other Operators

1) Arithmetic Operators:

                     Operator          Meaning

                           +                       Addition

-                                Subtraction

*                          Multiplication

/                        Division

%                      Modulo Division

Division operator returns quotient whereas modulo division operator returns remainder.

Example:

class Demo

{

          public static void main(String args[])

{

                     int a=5, b=3, c=2;

                     int d=a*b/c;

                     int e=a/b*c;

                     System.out.println(d);

                     System.out.println(e);

          }

}

2) Relational Operators:

                     Operator          Meaning

                     <                             Less than

                     >                             Greater than

                     <=                          Less than or equals to

                     >=                          Greater than or equals to

                     ==                          Equals to

                     !=                           Not equals to

Relational operators are also called as comparison operators also.

The above all operators returns boolean value.

3) Logical Operators:

                     Operator          Meaning

                     &&                         Logical AND

                     ||                           Logical OR

                     !                             Logical NOT

Logical AND operator returns true if both the expressions returns true, otherwise returns false.

Logical OR operator returns false if both the expressions returns false, otherwise returns true.

Logical NOT operators reverse the logical state.

There are three types of Operators:

    1)    Unary Operator

    2)    Binary Operator

    3)    Ternary Operator

Unary Operator:

An operator that operates on only one operand is called as unary operator.

Examples:

++a, a++, --a, a--, +a, -a, !a, .. etc.,

Binary Operator:

An operator that operates on two operands is called as binary operator.

Examples:

a+b, a-b, a*b, a/b, a%b, a<b, a>b, a<=b, a>=b, a!=b, .. etc.,

Ternary Operator:

An operator that operates on three operands is called as ternary operator.

Example:

Conditional operator(? :)

4) Increment & Decrement Operators:

                     Operator          Meaning

                           ++                               Increment

                            --                        Decrement

Pre IncrementExample:

class Demo

{

         public static void main(String args[])

         {         

                     int a=5;

                     int b=++a; 

                     System.out.println(a);

                     System.out.println(b);

         }

}

In the above example, value of a incremented by 1 then assigned to b because it is a pre increment operator.

Post Increment Example:

class Demo

{

         public static void main(String args[])

         {         

                     int a=5;

                     int b=a++; 

                     System.out.println(a);

                     System.out.println(b);

         }

}

In the above example, value of a assigned to b then a value incremented by 1 because it is a post increment operator.

5) Bitwise Operators:

                     Operator          Meaning

                     &                            Bitwise AND

                      |                            Bitwise OR

                      ^                            Bitwise XOR

                     <<                          Left Shift

                     >>                          Right Shift

                      ~                            tilde

Note: All bitwise operators operate on binary data.

6) Assignment Operators:

                     Operator          Meaning

                          =                        Normal Assignment

                         a+=b                  a=a+b

                         a-=b                  a=a-b

                        a*=b                  a=a*b

                         a/=b                  a=a/b

                         a%=b                 a=a%b

                         a&=b                 a=a&b

                         a|=b                  a=a|b

                         a^=b                  a=a^b

                         a<<=b               a=a<<b

                         a>>=b               a=a>>b

7) Conditional Operator(?:):

It is used to express the condition.

Example:

class Demo

{

         public static void main(String args[])

         {         

          int a=5, b=3;

          int c=(a>b)?a:b;

          System.out.println(c);

         }

}

In the conditional operator statement, if the condition returns true then a value stored in c otherwise b value stored in c.

8) Unary Plus & Unary Minus Operators:

Operator          Meaning           

+                             Unary Plus

                     -                             Unary Minus

9) Other Operators:

                     Operator          Meaning

                           []                      Array Operator

                           ()                      Type Cast Operator

                    instanceof           Instance Of Operator

                               .                   Member Selection Operator

                               ()                  Method Call Operator .. etc.,

By

Mr. Venkatesh Mansani

No comments:

Post a Comment