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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:30:01+00:00 2026-05-24T19:30:01+00:00

List l = new ArrayList<Number>(); List<String> ls = l; // unchecked warning l.add(0, new

  • 0
List l = new ArrayList<Number>();
List<String> ls = l;       // unchecked warning
l.add(0, new Integer(42)); // another unchecked warning
String s = ls.get(0);      // ClassCastException is thrown

The java documentation says;

Furthermore, a heap pollution situation occurs when the l.add method
is called. The static type second formal parameter of the add method
is String, but this method is called with an actual parameter of a
different type, Integer
. However, the compiler still allows this
method call. Because of type erasure, the type of the second formal
parameter of the add method (which is defined as List.add(int,E))
becomes Object. Consequently, the compiler allows this method call
because, after type erasure, the l.add method can add any object of
type Object, including an object of Integer type

my question is about the part i made bold. I understand that in line 2 heap pollution occurs cause l being of List references an object of type List. But when add method is called on l reference shouldn’t it expect Number type instead of String as l is reference to ArrayList. Or is it, that after ls=l the space in heap is made of List<String> also for the l reference? in this case makes sense what java docs says in the part made in bold

  • 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-24T19:30:02+00:00Added an answer on May 24, 2026 at 7:30 pm

    But when add method is called on l reference shouldn’t it expect Number type instead of String as l is reference to ArrayList.

    List l is just declared as an untyped List. So the compiler will let you put in whatever you like (and warn about it), and let you assign it to an incompatible List<String> variable (and warn about it).

    If you said List<Number> l, the compiler would enforce the proper types (and not let you assign it to ls).

    No matter what generic types you declare or not declare, this has no effect at runtime. To maintain backwards compatibility, generics are a complete compile-time-only feature.

    Or is it, that after ls=l the space in heap is made of List also for the l reference??

    If you say

    List l = new ArrayList<Integer>();
    List<String> ls = l;
    Object x = ls;
    Collection<?> c = (Collection) x;
    

    you have four different variables (with four different type declarations), but they all point to the same data (the same object on the heap).

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

Sidebar

Related Questions

/*1.*/ List l = new ArrayList<Number>(); /*2.*/ List<String> ls = l; // unchecked warning
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
In Java, i like to use constructs such as List<String> list = new ArrayList<String>()
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
private List<String> subList; private List<List<String>> records = new ArrayList<List<String>>(); for(....){ subList = new ArrayList<String>();
I've always been one to simply use: List<String> names = new ArrayList<>(); I use
public static List<Vertex<Integer>> petersenGraph() { List<Vertex<Integer>> v = new ArrayList<Vertex<Integer>>(); for (int i =
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i <
I have String arrays of arrays. List<String[]> mainList = new ArrayList<String[]>(); String[] row1 =

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.