In Java, how would I use generics to create a max function that takes as parameters two Comparable objects of the same type and returns the larger one?
I tried:
public static <T extends Comparable> T max(T obj1, T obj2)
{
return ( ((obj1).compareTo(obj2) >= 0) ? obj1 : obj2);
}
(It returns obj1 if they are both equal.)
The method is based on code I found at http://www.informit.com/articles/article.aspx?p=170176&seqNum=3.
However, when I compile it I get this warning (compiling with -Xlint:unchecked): warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable