Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler inferencing in generic methods

 
mayur dhawan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
mayur dhawan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope the problem is clear

any help is appreciated
thanks in advance
mayur
 
mayur dhawan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that there is promotion of type behind the scenes.But I can't understand how is it working.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic