Java Generic Example – Parameterizd Type Eexample

Hello Freinds

Here I will try to write small program which will enables you to view why generics are there in java.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package genericbasic;

/**
 *
 * @author Vishal
 * In this example, I will show why generic are there
 * first we write tree separate overloaded method to print
 * reference type array.
 * after that we write single generic method which can
 * perform same tast.
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here

        Integer[] intarray = {1,2,3,4,5};
        Double[] dblarray = {1.1,1.2,1.3,1.4};
        Character[] charray = {‘a’,’b’,’c’};

        System.out.println( “Array integerArray contains:” );
            printArray( intarray ); // pass an Integer array
        System.out.println( “\nArray doubleArray contains:” );
            printArray( dblarray ); // pass a Double array
        System.out.println( “\nArray characterArray contains:” );
              printArray( charray ); // pass a Character array

        Integer[] intarraygeneric = {1,2,3,4,5};
        Double[] dblarraygeneric = {1.1,1.2,1.3,1.4};
        Character[] charraygeneric = {‘a’,’b’,’c’};

              System.out.println( “Array integerArraygeneric contains:” );
              // How Calling Works
              /*
               *When the compiler encounters line  printArrayGeneric( intarraygeneric );
               , it first determines argument
               intarraygeneric’s type (i.e., Integer[]) and attempts to locate a
               named printArrayGeneric that specifies a single Integer[] parameter.
               There is no such method in this example. Next, the compiler
               determines whether there is a generic method named printArrayGeneric
               that specifies a single array parameter and uses a type parameter
               to represent the array element type. The compiler determines that
               method printArrayGeneric is a match and sets up a call to
               the method. The same process is repeated for the calls to method
               printArrayGeneric for double and character
               */
              
           // pass an Integer array
               printArrayGeneric( intarraygeneric );
        System.out.println( “\nArray doubleArraygeneric contains:” );
            printArrayGeneric( dblarraygeneric ); // pass a Double array
        System.out.println( “\nArray characterArraygeneric contains:” );
              printArrayGeneric( charraygeneric ); // pass a Character array
   
    }
    //Overloaded methods (printarray) are often used to perform similar
    //operations on different types of data.

    // method printArray to print Integer array
     public static void printArray( Integer[] inputArray )
       {
         // display array elements
          for ( Integer element : inputArray )
             System.out.printf( “%s “, element );

          System.out.println();
       } // end method printArray

     //Overloaded methods (printarray) are often used to perform similar
    //operations on different types of data.

// method printArray to print Double array
        public static void printArray( Double[] dblArray )
       {
         // display array elements
          for ( Double element : dblArray )
             System.out.printf( “%s “, element );

          System.out.println();
       } // end method printArray

    //Overloaded methods (printarray) are often used to perform similar
    //operations on different types of data.

// method printArray to print Character array
   public static void printArray( Character[] chArray )
       {
         // display array elements
          for ( Character element : chArray )
             System.out.printf( “%s “, element );

          System.out.println();
       } // end method printArray

   //Generic method

  // Compile will translate following method in below manner
   /*
    * check input type..its now Object – the root class
    1   public static void printArray( Object[] inputArray )
    2   {   
    3    
    4      for ( Object element : inputArray )   
    5      System.out.printf( “%s “, element );    
    6      System.out.println();   
    7   }
    */
   public static < E > void printArrayGeneric( E[] inputArray )
   {
       for ( E element : inputArray )
            System.out.printf( “%s “, element );
       System.out.println();
   } // end method printArray
}

most of the things are self explanatory, please copy and run the program to see output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: