Originally posted by mayur dhawan:
Hi,
thanks for the response.actually even if we do come comparision stuff inside the method.Then also it gives an error when we call it like
You can return x if x is greater than Y or else you can return y.
Object x = t.findLarger(123, "456");
thanks
mayur
Hello
Just so you understand the generic method declaration, let's look at your generic method in essence:
Comparable is an interface and when you look at the Type variable declaration <T extends Comparable> T will be an alias for any type that
IMPLEMENTS Comparable. Extends is used interchangeably for (literally) extends and implements.
Next we find that your generic method will return a reference of type T.
Finally your method has 2 parameters of some type T but you invoke it with 2 different argument types, an int and a
String. Im not an expert, but I would imagine that you have to invoke the generic method with 1 particular type in your method. Look at the return type 'T', what type is going to be returned if you invoke the method with 2 different types?
Comparable is used to define how instances of a class will be sorted when they are added to collections. When you invoke the sort method of Collections or Arrays, and your class doesn't implement the comparable method and also implement the compareTo method, you will get a compiler error. The collection's elements should also be 'mutually comparable' other wise you will get a runtime exception. Oh joy!. Take a look at this
Generics tutorial Best regards.
[ June 23, 2008: Message edited by: Keith Nagle ]
[ June 23, 2008: Message edited by: Keith Nagle ]