By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Generic Methods in Java

P: 45
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.

Expand|Select|Wrap|Line Numbers
  1. class GenericMethod
  2. {                      
  3.  public static<T> void print(T[] a)
  4.  {             
  5.   for(T elt:a)
  6.   {        
  7.    System.out.println(elt);
  8.   }
  9.  }
  10. }
  11. /**
  12. @author Sreekandan.K
  13. */
  14. public class GenericMethodDemo
  15. {
  16.  public static void main( String args[] )
  17.  {
  18.   GenericMethod gm=new GenericMethod();
  19.   Integer[] i={3,2,1,5,4};
  20.   System.out.println("Integer Array Elements:");
  21.   gm.print(i);
  22.   Double[] d={1.55,1.29,1.33,1.4};
  23.   System.out.println("Double Array Elements:");
  24.   gm.print(d);
  25.   String[] str={"C","D","A","E","B"};
  26.   System.out.println("String Array Elements:");
  27.   gm.print(str);
  28.  } 
  29. }
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.
Nov 1 '12 #1
Share this Question
Share on Google+
5 Replies

P: 1
This is helpful information.Thanks such a significant topics.
Dec 8 '12 #2

Expert Mod 5K+
P: 5,397
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 GenericMethodDemo at lines 21, 24, and 27 - a contrast/comparison of what you would have to do under "normal" circumstances vs. this method.
Dec 9 '12 #3

P: 45
Thank You very much for your comments
Dec 9 '12 #4

Expert Mod 5K+
P: 5,397
I have sent you a PM regarding this article.
Feb 17 '13 #5

Expert Mod 5K+
P: 5,397

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: (Updated)
Feb 23 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.