• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic