Generic Methods in Java 5.0

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.

also read:

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.

public <T> List<T> convertArrayToList(T[] array){
List<T> list = new ArrayList<T>();
for(T element : array){
return list;

here <T> is the generic type, the value of T can be determined according to the method call. 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

convertArrayToList(new String[]{”1″,”2″});

here the value of T is String.

convertArrayToList(new Integer[]{1,2});

here the value of T is Integer.

the complete code looks like this.

public static void main(String[] args) {
GenericMethod genericMethod = new GenericMethod();
List<string> sList = genericMethod.convertArrayToList(new String[]{”1″,”2″});
List<integer> iList = genericMethod.convertArrayToList(new Integer[]{1,2});
class GenericMethod{
public <t> List<t> convertArrayToList(T[] array){
List<t> list = new ArrayList<t>();
for(T element : array){
return list;

we can have the boundaries also in generic methods.

public <t extends Number> List<t> convertArrayToList(T[] array)

above method signature means we can call this method only with types which extends Number not with others types.
Let us see another method

public <T> T method(T x,T y){
return x;

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.

method(new String("string"), new Object());

then what T will be ?????? this is interesting. In this case compiler checks for the hierarchy and takes super type as here T will be Object.check below code

Comparable<integer> comparable = new Comparable<integer>(){
public int compareTo(Integer arg0) {
return 0;
genericMethod.method(comparable, new Integer(1));

here T will be comparable. 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<?> c = genericMethod.method(new Float(1), new Integer(1));

and see the below code.

public <t> T test(){
return null;

how to determine T because here we don’t have parameter. 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 Object



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind