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 ]
Joined: Jan 14, 2007
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
subject: Generics question, related to K&B chapter 7 question 14