• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic doubt

 
Ravi Yadav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am going through chap 7 of K&B. I dont understand the last self-test question.

..............
16. Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)
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 the above.
............


The correct answers are B, E, and F but when I tried to compile the program I got the following error message for all the choices:

"Type mismatch: cannot convert from List<capture#1-of ? super Number> to List<Number>"

None of the answers worked for me.

The only option that worked is -

public static <E extends Number> List<E> process(List<E> nums) -
prototype without "? super E"


Please let me know what am I doing wrong here.

Thanks and regards,
SCJPAspirant
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think you made an error when copying the question, because the "only option that worked" as you stated, is in fact the method described in the question :

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

With this method, the results are the one expected.

Gilles
[ March 02, 2008: Message edited by: Gilles Marceau ]
 
Ravi Yadav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marceau,

The question has an additional ? super E. Whereas the option that worked for me doesnt.

Am I missing something here?

regards,
SCJPAspirant
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the question with ? super E , is wrong! i mean not by syntax none of options satisfy. check K & B Errata topic.
 
Ravi Yadav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great to know.thanks kahn.
That boosts my confidence.
 
Ravi Yadav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can someone please tell me why C is not correct?
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K&B Errata topic what means that.
I am also reading but K&B i can't find.
Can anyone help me out. I also want to understand the concept.
Please
:roll:
 
Michael Swierczek
Ranch Hand
Posts: 125
1
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dinesh,

Errata is the list of errors. If "K & B" refers to Katherine Sierra and Bert Bates' book "Sun Certified Programmer for Java 5 Study Guide" then the Errata is available here:
http://www.mhprofessional.com/downloads/products/0072253606/0072253606_errata0504.txt
 
Michael Swierczek
Ranch Hand
Posts: 125
1
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Yadav,
The Errata of the function is:
public static <E extends Number> List<E> process(List<E> nums)

Answer C has: ArrayList<Number> input = null;
List<Integer> = null;

So the types involved in trying to compile line C are:
List<Integer> = process(ArrayList<Number> ;
You can implicitly get a List from an ArrayList, so that's fine.
But you can't get an Integer from a Number without some form of explicit conversion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic