In java 1.5 we have a feature called generic methods, with this we can write a method, which can be generic, means it can be called by any parameter and behavior of the method will be according to the parameter passed.
Assume that we need to write a method which takes an array and converts it to list.
assume that we want to do it with integers, we can write the method easily and what if we want to achieve the same functionality with Strings, do we need to write one more method???.
Not necessary in 1.5. we can achieve this easily with generic methods.
the syntax of method would look like this.
1 2 3 4 5 6 7 | public <tglt; List<tglt; convertArrayToList(T[] array){
List<tglt; list = new ArrayList<tglt;();
for(T element : array){
list.add(element);
}
return list;
} |
here
and notice that generic type should be placed before method return type, method return type and method name should be together.
now if we call the method using String array it will return String list. the syntax would be
the complete code looks like this. we can have the boundaries also in generic methods. above method signature means we can call this method only with types which extends Number not with others types. If we call this method like method(“test”,”test”), then T will be String that is straight forward, what happens if we call it like below. then what T will be ?????? this is interesting. In this case compiler checks for the hierarchy and takes super type as T. so here T will be Object.check below code here T will be comparable. how to determine T because here we don’t have parameter.1
2
3
4
5
6
7
convertArrayToList(new String[]{”1″,”2″});
here the value of T is String.
convertArrayToList(new Integer[]{1,2});
here the value of T is Integer.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) {
GenericMethod genericMethod = new GenericMethod();
List<stringglt; sList = genericMethod.convertArrayToList(new String[]{”1″,”2″});
System.out.println(sList.toString());
List<integerglt; iList = genericMethod.convertArrayToList(new Integer[]{1,2});
System.out.println(iList.toString());
}
}
class GenericMethod{
public <tglt; List<tglt; convertArrayToList(T[] array){
List<tglt; list = new ArrayList<tglt;();
for(T element : array){
list.add(element);
}
return list;
}1
public <t extends Numberglt; List<tglt; convertArrayToList(T[] array)
Let us see another method1
2
3
public <tglt; T method(T x,T y){
return x;
}1
method(new String("string"), new Object());1
2
3
4
5
6
Comparable<integerglt; comparable = new Comparable<integerglt;(){
public int compareTo(Integer arg0) {
return 0;
}
};
genericMethod.method(comparable, new Integer(1));
and what happens if we can like below
genericMethod.method(new Float(1), new Integer(1));
here compiler checks for super class that is extended by both Integer and Float or interface that is implemented by Float and Integer and then it will decide T value
in above case T can be Number or Comparable or can be Object
Number n = genericMethod.method(new Float(1), new Integer(1));
Comparable
and see the below code.1
2
3
public <tglt; T test(){
return null;
}
In these case T value depends on how we are capturing the value returned by method call
String s = genericMethod.test();
in above call T will be String
Object o = genericMethod.test();
and in above call T is ObjectComments






September 17, 2008
Java