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.
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.
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);
}
}
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.
Comment