Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Method in K&B Question 14

 
Karen Marie
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In playing with Question 14 in K&B I realized I'm still pretty fuzzy on the Generics Stuff. I've read other threads that explain in detail but something is not quite sinking in. Question:

Why does this compile:



But this does not:
 
Julio Eneriz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that you are defining twice E. This compiles:

public static <E extends CharSequence> Collection<E>
getLongWordsC(Collection<E> col){
return new ArrayList<E>();
}

The one with ? compiles because it's something different.
 
Karen Marie
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that you are defining twice E.


This also does not compile with the same error as the other, a squiggly on extends saying to remove it and a message that the return type is missing:

 
Karen Marie
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same thing here:



That's just to note that its not complaining that the value being returned is of the wrong type but that the return type is not declared correctly.

So why is it not correct?
 
Julio Eneriz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem now is that you are trying to use T without declaring it. Or, if Collection<T extends CharSequence> was a declaration (which is not), then the returning type is missing.

Besides, you cannot make a new Collection, because Collection is an interface.

I think what you are trying would be this:

public static <E extends CharSequence, T extends CharSequence> Collection<T> test(Collection<T> col){
return new ArrayList<T>();
}
[ November 30, 2007: Message edited by: Julio Eneriz ]
 
Karen Marie
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Starting to get clearer. So is there ever a case where you could see

<E extends Anything>

as part of the return type?
 
Julio Eneriz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karen Marie:
Starting to get clearer. So is there ever a case where you could see

<E extends Anything>

as part of the return type?


<E extends Anything> is not a valid return type, is the definition of what is E. Once defined, you can use it, something like that

<E extends Anything> E getThat() {...}

Or



I cannot find any useful use for that. I think the use of generics in return type happens when you define E in the class (for example class Draw<E extends Clothes> and something like <E> getFirstItem ).

Maybe someone with more experiencie will do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic