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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:40:54+00:00 2026-06-11T20:40:54+00:00

Here is my program. I am not sure why I am getting a compile

  • 0

Here is my program. I am not sure why I am getting a compile time error.

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
    List< ? extends Number > list = new ArrayList<Integer>();

    list.add(6); // Compile Time Error

    System.out.println(list);

  }
}

But the following program works fine

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
    List< ? super Number > list = new ArrayList<Number>();

    list.add(6); 

    System.out.println(list);

    }
}

Error from Eclipse:

Here is the error description from Eclipse:

The method add(int, capture#1-of ? extends Number) in the type List is not
applicable for the arguments (int)

  • 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-11T20:40:55+00:00Added an answer on June 11, 2026 at 8:40 pm

    It’s because what you are doing in the first case isn’t type safe. You have declared list as a List of “some subclass of Number“, and then tried to insert an Integer into it. There is absolutely no guarantee that Integer is compatible with the actual run-time type of the underlying list. The compiler stops you here because what you are doing doesn’t make any sense.

    Consider, as an extreme example:

    List< ? extends Object > list = new ArrayList<Integer>();
    list.add("Hello, World!");
    

    If this were to work, you would have a List<Integer> with a String in it!

    If you really want it to work, you have to tell the compiler you know what you are doing by casting:

    ((List<Integer>)list).add(6);
    

    But even then you will still get a warning about type safety.

    The second case works because the list is guaranteed to be “some superclass of Number”. Integer is a subclass of Number, so it can be implicitly converted to any superclass (including Number itself), so there is no risk that the value is incompatible with the actual type of the list.

    For further information, you may want to read up on the difference between covariance and contravariance.

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

Sidebar

Related Questions

Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
I am getting inconsistent datatype error message and I am not sure why. I
I keep getting an error when trying to compile my program, i'm having issues
I'm not sure what is the root cause for getting such error(Heap Corruption) from
Hi i've got here a simple program, but it's not working properly. When i
When building my C++ program, I'm getting the error message undefined reference to 'vtable...
I'm not sure why I'm still getting the PHP is not recognized as an
I'm getting that exception when I try to run a simple DirectSound program. Here's
I'm getting an error. Here's the code copied across to a Console project and
I just started learning java and I'm working on a program. I'm getting an

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.