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

  • SEARCH
  • Home
  • 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 8602969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:12:52+00:00 2026-06-12T02:12:52+00:00

Can anybody explain why we use ? in Collection generics. As for example :

  • 0

Can anybody explain why we use ? in Collection generics.

As for example :

 List<? extends Number> numberlist;
 List<? super Integer> numberlist;
  • 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-06-12T02:12:54+00:00Added an answer on June 12, 2026 at 2:12 am

    The wildcards introduce restrictions in how the collection can be used.

    For example, with List<? extends Number>, I can’t add new elements to the list. This is because all I know is that the list is some kind of subtype of Number, but I don’t know what that actual subtype is (so how could I know what to add?). For example, take the following code:

    public void doSomethingWith(List<? extends Number> numbers) {
        numbers.add(Integer.valueOf(0)); // Won't compile
    }
    

    This won’t compile because both of these method calls are legal:

    doSomethingWith(new ArrayList<Integer>());
    doSomethingWith(new ArrayList<Double>());
    

    What you can do is read elements from the list:

    // This will all compile
    public void doSomethingWith(List<? extends Number> numbers) {
        for (Number number : numbers) {
            // Do something with number
        }
        // OR
        Number number = numbers.get(0);
        // OR
        Number number = numbers.remove(0);
    }
    

    Calls to methods like get will return some kind of Number, we know that for a fact because of the ? extends Number, so we can treat it like that for reading purposes.

    On the other hand, List<? super Integer> has exactly the opposite result. I can no longer read from the list, but I can write to it. I know that whatever ? is, it will definitely be a super-class of Integer, so concrete types of the list will definitely accept Integer values. For example:

    public void doSomethingWith(List<? super Integer> integers) {
        integers.add(Integer.valueOf(0));
    }
    

    That code is completely legal. However, if you want to read from the list, the only way to do this is to use Object since anything else requires casting (which requires knowing its concrete type):

    for (Object obj : integers)
    // OR
    Object obj = integers.get(0);
    // OR
    Object obj = integers.remove(0);
    

    What’s Really Happening

    Here’s what’s actually happening. When you specify ? extends Number, you’re making any method that takes elements as a parameter unusable. In fact, if you try to auto-complete code in Eclipse using Ctrl+Space on a List<? extends Number>, it shows null as the parameters’ types in the add methods and the like. Meanwhile, all the methods that return elements are guaranteed to return at least some kind of Number, though you won’t know exactly which subclass of Number it might actually be.

    When you specify ? super Integer, you’re making any method that takes elements as a parameter guarantee that they’ll accept Integer values (and sub-classes of Integer as well). This allows you to call methods like add since you know they’ll accept Integer types. Meanwhile, all methods that return elements are only guaranteed to return something, but we don’t know what, so all the methods that return elements are only guaranteed to return Object.

    PECS is an excellent acronym to remember this, it means “Producer Extends, Consumer Supers”. This means that if you want your list to give you something, it’s a producer, and you should use extends. If you want your list to accept things from you, it’s a consumer, so you use super. See this answer for more.

    But what if I have a wildcard with no bounds?

    It does both! <?> restricts you from calling methods that take the generic type as an argument and causes all the methods that return the generic type to return Object. This is because we have no idea what the type is whatsoever. For example, all of these assignments into a List<?> are legal:

    List<?> list;
    list = new ArrayList<Integer>();
    list = new ArrayList<String>();
    list = new ArrayList<MyClass>();
    

    And so on.

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

Sidebar

Related Questions

Guys can anybody explain me this nature of new and the use of Integer
Can anybody explain the importance of NSNotificationCenter? Where to use them? What is the
Can anybody explain with examples about why should we use Thread-pools. I have know
Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The
Can anybody explain me, what is use of vector class? My Professor mentioned about
Can any body explain me what is a Context and how can i use
Can anybody explain me why below code is working on private member variable? public
Can anybody explain to me how objects are stored and removed from heap memory
Can anybody explain what does JdClient is in this code: public JdClient client =
can anybody explain this why its happening int i=0; i=i++; i=i++; i=i++; System.out.println(i); it

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.