• 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

some Collection and Generics doubts

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Page 617 K&B Qno.12 Why is the answer The code does not compile?
Page 620 K&B Qno.16 Please explain how is this ques evaluated?






Edited: no need to shout.
[ September 11, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Page 617 K&B Qno.12 Why is the answer The code does not compile?



You cannot add a reference to an Object to a HashSet<Person>. You may only add a Person (or a subclass of Person).


Page 620 K&B Qno.16 Please explain how is this ques evaluated?



The return type is defined to be a List. You can't assign a List to an ArrayList reference. (The List reference may point to a List that is not of type ArrayList such as LinkedList.) So A and D are wrong.

C is wrong because output is List<Number> but the reference returned is a List<Integer>. Java can't convert the reference like that.
 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q12 Is n't (new Person("Jane")) an object?
Q16 Why answer C is wrong?When you substitute Integer in
public static<E extends Number> List<? super E>process(List<E>nums)
then you get Number(ie ? super Integer) or is there any difference b/w <E> and ? super E
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the K & B errata:

620....bug.......Q-16: method declaration should be:
& 634
public static <E extends Number> List<E> process(List<E> nums)


[ September 11, 2006: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding question 12: have you compiled the code?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q12 Is n't (new Person("Jane")) an object?



Yes. But again, you can't add an Object reference to a HashSet<Person>. Inside the add(Object) method where super.add() is called, the reference is a reference to an Object. (At run time the actual object will be a Person, but the compiler doesn't know that.)
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic