aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compiler inferencing  in generic methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compiler inferencing  in generic methods" Watch "compiler inferencing  in generic methods" New topic
Author

compiler inferencing in generic methods

mayur dhawan
Greenhorn

Joined: Sep 20, 2005
Posts: 29
hi,
If we have a method
public <T extends Comparable> T findLarger(T x, T y) {}

and I invoke it as findLarger(123, "456");
then the compiler infers it as
<? extends Object> ? extends Object com.generics.Test.findLarger(? extends Object x, ? extends Object y)

Can you explain why?

thanks in advance
mayur
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Hi there. Your method invocation is wrong.
Your generic method returns a type T and you don't explicitly return anything from your generic method.
It's up to you to decide what logic is applied within the method and what will subsequently be returned.
That code will never compile as is.
I get the following error when I run your method



SCJP 5.0
mayur dhawan
Greenhorn

Joined: Sep 20, 2005
Posts: 29
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
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
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 ]
mayur dhawan
Greenhorn

Joined: Sep 20, 2005
Posts: 29
Hi,
following is the code
public <T extends Comparable> T findLarger(T x, T y) {
if(x.compareTo(y) > 0) {
return x;
} else {
return y;
}
}
public static void main(String[] args){
Test t = new Test();

Object x = t.findLarger(123, "456");
}
This code compiles and runs .In an IDE when I browse over the line Object x = t.findLarger(123, "456");
I see that the compiler has converted the function to
<? extends Object> ? extends Object com.generics.Test.findLarger(? extends Object x, ? extends Object y)
I want to know why is it happening.The code is compiling fine.

thanks
mayur
mayur dhawan
Greenhorn

Joined: Sep 20, 2005
Posts: 29
I hope the problem is clear

any help is appreciated
thanks in advance
mayur
mayur dhawan
Greenhorn

Joined: Sep 20, 2005
Posts: 29
I think that there is promotion of type behind the scenes.But I can't understand how is it working.
 
wood burning stoves
 
subject: compiler inferencing in generic methods