Generics Type issue

Posted by JohnJohnGa on Stack Overflow See other posts from Stack Overflow or by JohnJohnGa
Published on 2011-11-14T17:49:03Z Indexed on 2011/11/14 17:50 UTC
Read the original article Hit count: 206

Filed under:
|
ArrayList<Integer> arrI = new ArrayList<Integer>();
ArrayList arrO = arrI; // Warning
/*  It is ok to add a String as it is an ArrayList of Objects
    but the JVM will know the real type, arrO is an arrayList of
    Integer... 
*/
arrO.add("Hello"); 
/*  How I can get a String in an ArrayList<Integer> ?? 
    Even if the compiler told me that I will get an Integer!
*/
System.out.println(arrI.get(0));

Anybody can explain what's happening here?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics