Generic Methods in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreekandank
    New Member
    • Jun 2009
    • 45

    Generic Methods in Java

    In java, generic method is a special type of method that can be used for different types of data. You can write a single generic method that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, compiler handles each method call appropriately. This approach eliminates the method definition for various return types.

    Generic methods would be important if we could write a single method that could read the elements in a Integer array, a Double array, a String array or any types of array. Generic methods can be defined either inside ordinary classes or inside generic classes.

    Let's consider the following example program that defines a single generic method and shows how to read the elements from Integer array, Double array, and String array.

    Code:
    class GenericMethod
    {                      
     public static<T> void print(T[] a)
     {             
      for(T elt:a)
      {        
       System.out.println(elt);
      }
     }
    }
    /**
    @author Sreekandan.K
    */
    public class GenericMethodDemo
    {
     public static void main( String args[] )
     {
      GenericMethod gm=new GenericMethod();
      Integer[] i={3,2,1,5,4};
      System.out.println("Integer Array Elements:");
      gm.print(i);
      Double[] d={1.55,1.29,1.33,1.4};
      System.out.println("Double Array Elements:");
      gm.print(d);
      String[] str={"C","D","A","E","B"};
      System.out.println("String Array Elements:");
      gm.print(str);
     } 
    }
    In the above example program, we are having only one generic method print(T[] a) that operates on various types of parameters depends on the function call.

    Here, T indicates a type variable, that is common for any types of variables. This type variable T act according to the type of the parameters received.

    In the above example at line number 19, one integer array variable i is defined and at line number 21, we are calling the generic method print() with an integer array variable i [that is, print(i)]. Hence, the Generic method print(T[] a) accept an array of integer values.

    At line number 24, we are calling the same generic method with an double array variable d, so the generic method print(T[] a) handles an array of double variables.

    And also we are calling the same generic method at line number 27, with an array string variables, so the same generic method handles an array of string variables.
    Last edited by zmbd; Feb 23 '13, 07:23 PM. Reason: [z{second post is an extension of the original article.}{added some paragraph formating to the second post.}]
  • daroweyn
    New Member
    • Nov 2012
    • 1

    #2
    For details http://SamuraisSEO.com

    This is helpful information.Tha nks such a significant topics.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      That's very interesting; however, as this is an insight article, would you be willing to walk us thru the code on a line by line basis?

      Something along the lines of why Class GenericMethod is special as compared to simply calling those same lines of code within the class GenericMethodDe mo at lines 21, 24, and 27 - a contrast/comparison of what you would have to do under "normal" circumstances vs. this method.
      Last edited by zmbd; Feb 13 '13, 07:33 PM.

      Comment

      • sreekandank
        New Member
        • Jun 2009
        • 45

        #4
        Thank You very much for your comments

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          sreekandank:
          I have sent you a PM regarding this article.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            @sreekandank:

            I've merged the second post you made with the first as it is supposed to be an explanation of the original document. In doing so, I also broke the one solid mass of text into, IMHO, some smaller chunks. The average human eye needs a little "white space" on the page to help track the text and it helps to keep the intended thoughts together.

            While the expanded text does help with this article. It is still missing some critical information related to Generic Methods; thus, for those interested in using generic methods in their code, more information and an additional example is available in the ORACLE documentation:

            -

            Comment

            Working...