File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic doubt" Watch "Generic doubt" New topic
Author

Generic doubt

Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
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

Joined: Feb 17, 2007
Posts: 78
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 ]

SCJP 1.5<br />SCJD 1.6<br />SCBCD in progress...
Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
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

Joined: Nov 27, 2007
Posts: 108
yes the question with ? super E , is wrong! i mean not by syntax none of options satisfy. check K & B Errata topic.


<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
Great to know.thanks kahn.
That boosts my confidence.
Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
Hi,
can someone please tell me why C is not correct?
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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:


Thanks<br />Dinesh
Michael Swierczek
Ranch Hand

Joined: Oct 07, 2005
Posts: 117
    
    1

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

Joined: Oct 07, 2005
Posts: 117
    
    1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic doubt