简体   繁体   中英

unable to get the working of list collection in below scenario

May be this questions is already asked, but i didn't found any words about this,

Problem: I am not getting the working of below code. Could any one please add some points in order to understand this

public static void main(String[] args) {


   List list=new ArrayList<>(); 
   list.add("String");
   listOPt(list);
}
public static void listOPt(List<Integer> intList){
        intList.add(343);
        System.out.println(intList.toString());
}

output: [String, 343]

I am not getting how intList printing string also.

As per my understanding if we add any object before calling listOPt() it will insert into intList and after that function intList accept Integer only is it?

but how it is stored in memory?

Generics were implemented quite lately in the JDK so it was decided to use Type Erasure .

This means roughly that generics are checked at compile time but the information is lost at runtime. Casting a List<? extends Object> List<? extends Object> to a List<Integer> will certainly not remove the String item you added. In other cases you could have a compilation error (ie if oList was a List<String> ).

At below line,

List<? extends Object> oList=new ArrayList<>();

java will create a list which can take Object or any subclass of object.

At method listOPt(List<Integer> intList) , you intend to use type inference, as you have used <> operator at instantiation. But type inference works at the time of instantiation, where it has created a list which can accept Object and subtype. So in method listOPt(..) it can accept integer.

Basically your List object is as good as List<Object> .

Java compiler compiles with type erasure. Means when you compile code it will be compiled without your added generic.Please refer Java generics - type erasure - when and what happens and oracle Java compiler Type erasure

if you still want that your list should accept Integer then please declare it with generics

List list=new ArrayList();

Then you will not be able to add String in it.

Here in your code :

List list=new ArrayList<>();

You have not used any Type Erasures for the declaration of the variable list . So the compiler actually converts it to a RAW TYPE . Which practically means that now this list variable can be converted to any type of list like you have done List<Integer> and the compiler is unable to check the content of the the variable as you have not declared any Type Erasures.

So, immaterial of what has been earlier stored in the variable list the compiler simply treats list as an instance of List<Integer> . Now when you call intList.toString() it basically calls the toString method of all the objects that are contained in this list object and so you get that output.

This is basically the problem you run into while using Raw Type . In order that this kind of problem does not happen always use Type Erasures .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM