• 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

Doubt related to use of wild card character ( ? super E)

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Everyone,

I have a doubt in getting through question no 16. from K&B (page 634)

Given a method as

public static <E extends Number> List<? super E> process(List<E> nums)

A programmer want to use this method like this
// INSERT DECLARATIONS HERE

output=process(input);

which pair of declarations could be placed at //INSERT DECLARATION HERE to allow the code to compile.

A. ArrayList <Integer> input=null
ArrayList <Integer> output=null

B. ArrayList <Integer> input=null
List <Integer> output=null

C. ArrayList <Integer> input=null
List <Number> output=null

D.List<Number> input=null
ArrayList <Integer> output=null

E. List <Number> input=null
List <Number> output=null

F.List <Integer> input=null
List <Integer> output=null

G. None of these

Correct answers are: B,E and F

I have doubt related to C

ArrayList <Integer> input=null
List <Number> output=null

As the return type in the method declaration states

public static <E extends Number> List<? super E> process(List<E> nums) that means we can have super class of the value for E as generic type of List as return value.

so if i have ArrayList <Integer> input ,so corresponding to Integer for place holder E why can't i have List <Number > output as return value.

Please clarify in detail as i am not able to get the reason.


Regards

Jolly
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also doubt about this question because I think correct answer is option G
because all other option are not accepted by compiler when I consider each options given

I think following is correct for variable declaration type
1.List<?>
2.List<? super E> where E is type we pass in as argument to method
3.List of raw type

Option G is correct answer

Author of this book i.e. author of SCJP study guide
please clear doubt on this problem
[ June 08, 2008: Message edited by: Ninad Kulkarni ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jolly tiw:
Hi! Everyone,

I have a doubt in getting through question no 16. from K&B (page 634)

Given a method as

public static <E extends Number> List<? super E> process(List<E> nums)



The question is not that though, its :

public static <E extends Number> List<E> process(List<E> nums)
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Buddy i came across this link,

do have a look

https://coderanch.com/t/252493/java-programmer-SCJP/certification/Tough-Generics-book
reply
    Bookmark Topic Watch Topic
  • New Topic