File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jolly Tiwari
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mustafa Musaji
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buddy i came across this link,

do have a look

http://www.coderanch.com/t/252493/java-programmer-SCJP/certification/Tough-Generics-book
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic