• 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

Chapter 7 Question14 : wrong answer

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I'm confused with the answer given in the K&S book for the question 14 of the chapter7 (Generics). here is the explanation given in the book :

(B is wrong because longWords has been declared as a Collection<E>, and that can't be implicitly converted to a List<E> to match the declared return value. (Even though we know that longWords is really an ArrayList<E>, the compiler only know what it's been declared as.
)

for me the answer B is correct and here's an example :

---------------------------Code-----------------------------------------------
public class Chapter7Question14 {

public static void main(String[] args) {

String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}

public static <E extends CharSequence> List<E> getLongWords(Collection<E> col){

return new ArrayList<E>();
}

}
---------------------------------------------------------------------------

I'm Right ?

[ November 24, 2007: Message edited by: tarik el berrak ]
[ November 24, 2007: Message edited by: tarik el berrak ]
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collection to List is a downcast, so the compiler won't accept it without an explicit cast. List to Collection can be done implicitly.
 
tarik el berrak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure,
but in this example, the getLongWords return a List<E> (E is a String in this case), so It can be affected to a Collection<E> polymorphically (implicit cast)

the piece of code compiles and run fine (correspond to anser B), so for me the B is correct. Can you explain more please ?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's where I think you're getting confused: the problem is not with the return value of getLongWords() getting assigned to resultList. Rather, within getLongWords(), the longWords local variable is declared as a Collection<E>, so it cannot be returned as a List<E> without an explicit cast. You'd need to change the return statement to "return (List<E> longWords;" to make that work.
 
tarik el berrak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I forget the method body and focused on method signature. You are right, since the return is a collection<E>, it cannot be assigned to a List without explicit cast.
I have just question, is there a strategy to follow when answering the exam questions: for example, if within the answers proposed we find "Compilation fails", we have to look for errors first and then try to see what the code is suppposed to do if it compiles fine ??

Thanks in advance
 
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic