Operators

OPERATORS


The operations in java are represented by Operators and the objects of operation are called Operands. Java has a rich set of operators which includes:-

  1. Arithmetic operators - These operators are used for arithmetic operations. The five basic arithmetic operations are addition, subtraction, multiplication, division and modulus. There are 2 types of arithmetic operators which includes:-    
  • Unary - It includes unary + and - . For example:-
                     int a= 5;
                     int b= +a;
                     int c= -a;
       Here b will store +5 or simply 5 while c will store -5. So unary operators are used to change the sign and are always used against 1 operand.
  
  •  Binary - It includes binary +, -, *, /, %.  These operands are used for addition, subtraction, multiplication, division and modulus.
    2. Increment/Decrement Operators - The ++ is increment operator while -- is decremment                   operator. For example:-
              int a=5;
              a++;       or         ++a;
              int b=6;
              b--;         or          --b;
      Here a++ or ++a both are similar to a=a+1
      Similarly b-- or --b are similar to b= b-1
     Therefore a will store 6 while b will store 5
     
    Now there are two types of increment/decrement operators ;-
  •   Preincrement/Predecrement -   The ++a or --b were the preincrement/predecrement operators. In this case 1st the the values are incremented or decremented then the operation is done. For example:-
                   int a=5,b=6;
                   int c=(++a)+b;
                   int d=(--b)+a;
          Here the operation is addition. Thus
                        c=(++5)+6
                         = 6+6
                         =12
       Therefore a=6,b=6,c=12. Again,
                        d=(--6)+6      [Note:- The value of a has been changed from 5 to 6]
                          =5+6
                          =11
      Therefore d=11,a=6,b=5


   3.  Relational Operators - Relational operators refer to relationship that values can have with one  another. Java provides six relational operators < (less than) , >(greater than),<=(less than or equal to), >=(greater than or equal to), ==(equal to equal to), !=(not equal to). 


   4. Logical Operators - Java supports 6 logical operators namely &(and), |(or), ^(xor), &&(and and), ||(or or), !(not).
       

&& is similar to & while || is similar to |   





    5. Assignment operator : - There is one assignment operator which is =.  This operator is used to assign a value to a variable. For example:-
                        int a=5;
       Here 5 is the value which is assigned to a.

   6. Shift Operator :- The three shift operators are >> (signed right shift), <<(signed left shift), >>> (unsigned right shift). Let us take an example :-
            1101>>2=?

       Now let us take the example for 1101<<2 =?
   
     Finally let us consider 1101>>>2=?
          

   7. Shorthand operators :-  Java offers special shorthand operators that simplify the coding of a certain type of assignment statement. For example :-
                a=a+10;
            can also be written as 

                a+=10;

  8. Conditional Operator - Java provides a conditional operator which provides a value according to the given condition. The syntax is :- 
      condition ? statement1 : statement2 ;
      Here if the condition given is true, then statement 1 is executed and if the condition is false then the statement 2 is executed.

     For example :-  
     int a=5,b=6;
     a>b?System.out.println(a):System.out.println(b);

     Here the output will be 6.

Comments

Popular Posts