21 August 2012

Java Tutorial #4

Operator and expressions
    Arithmetic operator (+,-,*,/,%)
    These can operate on any build in numeric data-type of java except on boolean type ('true' , 'false') . All the arithmetic operators work in the same way as they use to do in C or C++
    Modulo operator
    Modulo operator mainly gives the remainder of two numbers division.
             Always keep in mind that sign of the result in modulo arithmetic is always the sign of first operand (or dividend)
    e.g=> -14%3=-2
               -14%-3=-2
                14%-3=2
    Unlike C,C++ modulo operator % can be applied to the floating point data as well
    The floating point modulus operator returns the floating point equivalent of an integer division.
                     It simply means that ,the division is carried out with both the floating point operand, but the resulting divisor is treated as integer & resulting in floating remainder will acts as the output of the modulo division of floting point numbers
    When arithmetic operation is performed on two different types of data values,the resultant will always be converted into higher precision value
Logical operator
    &&
    Logical and
    can be true only when both the values across && operator should be true
    ||
    Logical or
    can be true only when atleast any one the values across || operator should be true
    !
    Logical not
    false if expression is anything other than 0
Special operators
    Instanceof opertaor
    It is an object reference operator and returns "true" if the object on LHS is an instance of the class on the RHS
    This opertor mainly help us to determine whether an object belongs to a particular class or not
              e.g=> person instanceof student
                         will be true if person belongs to the class of student otherwise it will return false
    Dot operator
    is mainly used to access the instance variable & method of class objects
     e.g=>person.age ;                    //refernce to variable age
          person.salary();                //refrence to the method salary
    
    Remember java doesnot have any operator for exponentiaion (or power), in place it have a method function in math class called pow(a,b) that will implement the same work as being done by the operator, if it would have been.(i.e=>a^b or, a to the power b);
    y=(int)(a+b)
    here the result is being converted into int value
    y=(int)a+b
    here a is convered into int value & then being added with b , so the resultant may not be an int value.

Generic typecasting
Typecasting is an unsafe operation because the compiler cannot check the wrong typcast. The compiler throws an exception if typecast fails at runtime
When using generics, the compiler insert typecast at appropriate places to implement type casting. So typecast become implicit in palce of explicit. Generics also detremine the typecast error at compile time rather than runtime
syntax
            Class SampleGenerics
            {
             sample code;
            }
            
e.g=>Illustration of use of generic type in collections
______________________________________________________________________________
______________________________________________________________________________
Following example illustrate how we can print array of different type using a single Generic method:

public class GenericMethodTest
{
   // generic method printArray                         
   public static < E > void printArray( E[] inputArray )
   {
      // Display array elements              
         for ( E element : inputArray ){        
            System.out.printf( "%s ", element );
         }
         System.out.println();
    }

    public static void main( String args[] )
    {
        // Create arrays of Integer, Double and Character
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

        System.out.println( "Array integerArray contains:" );
        printArray( intArray  ); // pass an Integer array

        System.out.println( "\nArray doubleArray contains:" );
        printArray( doubleArray ); // pass a Double array

        System.out.println( "\nArray characterArray contains:" );
        printArray( charArray ); // pass a Character array
    } 
}
_______________________________________________________________________________
_______________________________________________________________________________


    Selected important math function
    Math functions are the part of java.lang Package
    atan2(x,y) =>returns an angle whose tangent is x/y (i.e=> it is tan-inverse
                                                                                                   function)
    exp(x)=>returns e raised to x
    rint(x)=>returns truncated value of x
    abs(a)=>returns absolute value of a
    Syntax
    Math.function_name();
    eg=>Math.sqrt(x);

No comments:

Post a Comment