aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics question, related to K&B chapter 7 question 14 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics question, related to K&B chapter 7 question 14" Watch "Generics question, related to K&B chapter 7 question 14" New topic
Author

Generics question, related to K&B chapter 7 question 14

Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hello,

I think I understand why F is the correct answer in this question. However, I would think that also

public static <T extends CharSequence> Collection<T> getLongWords(Collection<T extends CharSequence> coll)

ought to work - butit doesn't (compiler error). Could anyone please give me a hint what is wrong here?

Thank you very much,
Sigrid
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Sigrid,


public static <T extends CharSequence> Collection<T> getLongWords(Collection<T extends CharSequence> coll)


<T extends CharSequence>
Declaring T type
T is CharSequence or subtype of it

Collection<T>
Method returns Collection<T> parameterized type

Collection<T extends CharSequence>

Collection parameterized with T (that is CharSequence of subtype of it), that is passed to the method.


Thanks,
cmbhatt


cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why doesn't it compile?

public static <T extends CharSequence>
Collection<T> getLongWords(Collection<T extends CharSequence> coll)

You have already specified the bound on T at the beginning of the method declaration, you cannot refine it (or respecify it) in the method's argument. This is OK:

public static <T extends CharSequence>
Collection<T> getLongWords(Collection<? extends CharSequence> coll)

Well, that gets the compilation of that line fixed anyway. I have not checked whether it makes sense to do so in the context of the full question.
[ April 15, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
both replies make the code compile.

I think Sigrid is unclearof the difference between typesafeing a collection and templating a method.

when you are declaring a type safe collection you can use notation like:
ArrayList<? extends CharSequence> ,

but when you are templating a method or a class you use notation like <T extends CharSequence>

also be careful of the the method below,
the collection passed to the method will not determine what T is.
It is left to the return type to do this. ie you could pass in Collection<StringBuffer> and return Collection<String>.

public static <T extends CharSequence>
Collection<T> getLongWords(Collection<? extends CharSequence> coll)


in the method below the type of collection passed and returned would be the same:

public static <T extends CharSequence>
Collection<T> getLongWords(Collection<T> coll)

[ April 15, 2007: Message edited by: Louis Moloney ]

Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hello,

please excuse my answering you so late, and thanks a lot for your help! I think I'm getting closer now; you're right about my being not so clear about the difference between type (T) and wildcard (?) usage.
But now I seem to understand that T is defined in one place only (before the return type) and then serves to establish connections between input and return type. So, in the exam example,



does not really compile when you take the method body into account:



because I don't get a T out of my input collection, and when I try with



it does not really get better because I can't add a CharSequence to a Collection<T> (even though a Collection<T> IS a Collection of things extending CharSequence.
So, I conclude it really is the connection established by T usage which counts, not "actual" types. Now, writing all this down it seems rather curious to me I did not get that before... but somehow generics are my hardest topic...
Thanks again,
Sigrid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics question, related to K&B chapter 7 question 14