Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

doubt in generics example from kathy sierra

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

The correct answers are:

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

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

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


Doubt:
we can assign List<E> type to List<? super E> type
but can we assign conversly List<? super E> to List<E> as we are doing in above example because the method "process" is returning List<? super E> and in correct answers we are assigninig this returning value "List<? super E>" to a "List<E> output" refrence?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Kaushal,

seems to be your first or second posting here so

Welcome to the Ranch!



You were right in saying that we cannot assign a List<? super E> to a List<E>.
But we are not doing that in the example.
The return type is List<? super E>
and we return an List<E> in all three examples.
But this is
assigning a List<E> to a List<? super E> and not
assigning a List<? super E> to a List<E>.

And <? super E> also include E itself, but I'm sure you knew that.

Yours,
Bu.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see the errata:

K&B SCJP Errata

Thanks,
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic