• 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 on a K & B SCJP5 guide question: GENERICS

 
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

The following is a question from K & B SCJP5.0 guide:

Given a method declared as:

A programmer wants to use this method like this:

Which pair 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.

Correct Answers given are: B, E, F

But I thought the answer would be G because the method returns a List<? super E> where E is the element type of the List which was passed as argument to the method process & hence if:
'input' is List<Integer> then 'output' should be a List<? super Integer>.
Someone please help me with this question I think I got confused somewhere; but dont understand where (
Thanks in advance!
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

There is a little typo in your declaration its supposed to be:


I am having the same confusion about this question. The correct answer is B,E,F and the explanation in the book states:
"The return type is definitely declared as List, NOT ArrayList so A,D are wrong. ......"

This is what I don't get...why it is that the return type MUST be List only and not ArrayList?? Just like the
argument can be ArrayList then why cant return type also be arrayList?
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is interesting , I found out there is a mistake in the question specified in teh K&B errata link which
is present once we are in the SCJP forum, the below is what it mentions:

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

In my version of the book the method is declared as I have specified in my previous post...can someone
please let me know what the errata is?? There appears to be a mistake in the question itself. Please
help SCJP is complicated enough as it is, we don't need errors in questions to make matter worse
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct declaration is public static <E extends Number> List <E> process(List <E> nums)
BEF will compile
with ... List <? extends E>... nothing will compile

to Meghna:
you can return ArrayList if return type is List, but in this case you need cast it to (ArrayList) before you can assign it to variable of type ArrayList, but output = process(input); doesn't provide any casting, so it will not compile in this case.
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Danilou wrote:correct declaration is public static <E extends Number> List <E> process(List <E> nums)
BEF will compile
with ... List <? extends E>... nothing will compile



The book I have, has the question as I stated initially: public static <E extends Number> List<? super E> process(List<E> nums)

So in this case, the answer should be G. None of the above; Am I correct?
 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Meghna Bhardwaj wrote:HI,

There is a little typo in your declaration its supposed to be:




Hi Meghna, I have corrected my initial post; it was typing mistake the question in my version of the book has the method process return List<? super E> not List<E>; but I think the answer given (B E & F) are for List<E>
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic