Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 142163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:53:34+00:00 2026-05-11T07:53:34+00:00

I wrote this simple test code, by adapting a piece from a book, to

  • 0

I wrote this simple test code, by adapting a piece from a book, to help me understand the working of generic methods in Java. The line that I marked is supposed to work, why isn’t it?

import java.util.*;  class Test { public static void main(String args[]){     List<Number> input = null;     List<Number> output=null;      output = Test.process(input); // fail, why??      }  public static <E extends Number> List <? super E> process (List <E> nums){      List <E>eList = new ArrayList<E>();     return eList;     }  } 

edit: Well, the code works when changing List<? super E> to List<E>, that’s great, but I’m still not sure if I may have missed something or if the book I took this from may have a mistake. Please read the question as it appears there:

  1. Given a method declared as:

public static < E extends Number > List < ? super E > process (List 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. 

Answer: B, E, and F are correct. The return type of process is definitely declared as a List, not an ArrayList, so A and D are wrong. C is wrong because the return type evaluates to List<Integer>, and that can’t be assigned to a variable of type List<Number>. Of course all these would probably cause a NullPointerException since the variables are still null—but the question only asked us to get the code to compile.

None of the options mentioned on the book have worked on my code. Can I safely mark it as a mistake on Kathy Sierra’s part or am I missing something here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:53:35+00:00Added an answer on May 11, 2026 at 7:53 am

    The problem is that you are declaring the return type of your method as

    List<? super E>, i.e. List<? super Number> 

    but assigning it to a

    List<Number> 

    So you should either assign it to a

    List<? super Number> 

    or declare the return type as

    List<E> i.e. List<Number> 

    (Verbose formatting because inline code does not show generics correctly.)

    If the book claims assigning the return value of the method is possible in the way you describe it then indeed it seems to be a mistake in the book. It would be correct if the method returned a List of E (as above).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It really depends what you mean by "stops/delays the app."… May 11, 2026 at 10:03 pm
  • Editorial Team
    Editorial Team added an answer Arrays have .Length, not .Count. But this is available (as… May 11, 2026 at 10:03 pm
  • Editorial Team
    Editorial Team added an answer Since you mention SVN, you could use externals to "import"… May 11, 2026 at 10:03 pm

Related Questions

So it seemed easy enough. Use a series of nested loops to go though
I'm writing a unit test for a method that packs boolean values into a
When I write an app, I use the System.Data interfaces (IDbConnection, IDbCommand, IDataReader, IDbDataParameter,
I'm creating a GUID for use in a Classic ASP application, by using TypeLib.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.