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 7687641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:44:55+00:00 2026-05-31T19:44:55+00:00

In the book Java Generics and Collections by Maurice Naftalin, Philip Wadler, I was

  • 0

In the book Java Generics and Collections by Maurice Naftalin, Philip Wadler, I was going through Generics limitations and came up with doubt. May be that is answered in the book, but I think I am confused a loy.
In the following code:

 List<List<?>> lists = new ArrayList<List<?>>();
 lists.add(Arrays.asList(1,2,3));
 lists.add(Arrays.asList("four","five"));
 assert lists.toString().equals("[[1, 2, 3], [four, five]]");

As is said in the book, that nested wildcards instantiation has no problem, because for the first list , it knows that it will contain objects of list types.

But I tried to modify the above code and came up with one warning and one compile time error. I tried to do:

    List<?> sample= Arrays.asList(1,2,3,4.14);
    List<List<?>> lists = new ArrayList<List<?>>();
    lists.add(Arrays.asList(1,2,3));
    lists.get(0).add(5);
    lists.add(Arrays.asList("four","five"));
    System.out.println(sample.toString());
    assert lists.toString().equals("[[1, 2, 3], [four, five]]");

My questions are:
1) In the first line if I write:

   List<?> sample= Arrays.asList(1,2,3);

No warning is issued here but as written in the previous block, if I write:

   List<?> sample= Arrays.asList(1,2,3,4.14);

a warning is issued. Why?

2) Why is there a compile time error in fourth line:

   lists.get(0).add(5);

Thanks in advance.

  • 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. Editorial Team
    Editorial Team
    2026-05-31T19:44:56+00:00Added an answer on May 31, 2026 at 7:44 pm

    There is a compile time exception because lists.get(0) returns a List<?>

    You don’t know what is the type of this list, you know you can get elements from it (it will be at least an Object) but you can’t put anything in it (as you’re not sure that it will fit.

    What would append if you wrote instead:

    List<List<?>> lists = new ArrayList<List<?>>();
    lists.add(new ArrayList<String>());
    lists.get(0).add(5);
    lists.add(Arrays.asList("four","five"));
    

    You have an ArrayList<String> and you’re trying to add a number in it. So just to avoid this kind of mistake (and because the verification is done at compile time), you can’t add things if you’re not sure it will work.


    Regarding your warning, I don’t have any.

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

Sidebar

Related Questions

I’m just really learning Java, going through tutorials in book Java how to Program
In his book Java Design , Peter Coad says that one of the five
I am reading a book for Java that I am trying to learn, and
I was reading a book on Java Servlets where I came across HTTPSessionActivationListener .
I have read in a Java book that says: Because a String is immutable,
In core Java book it says The width of the rectangle that the getStringBounds
In my Java book there is an exercise that ask you to : Allocate
In the book Effective Java, Josh Bloch says that StringBuffer is largely obsolete and
I am reading a book about Java and it says that you can declare
I have a doubt from the book Efective Java. The doubt is regarding equals

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.