Language: Java
Compiler version: 1.6

In the below code, am trying to do the following:

  1. create a List<String>
  2. add a String
  3. assign List<String> to raw List
  4. create a List<Integer>
  5. assign the raw List to List<Integer>
  6. add an Integer
  7. retrieve the value using get() @ indexes 1 & 2 and print them.

All statements are compiling (with warnings) and run fine.

But if I try to loop through the List<Integer> using a for loop, I am getting a ClassCastException. I am just wondering why its allowed me to use list.get() method but not allowing me to iterate over it?

Output: (if I run with un-commented for loop) abcd 200

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot  be cast to java.lang.Integer
        at genericsamples.CheckRawTypeAdd.main(CheckRawTypeAdd.java:26)

Here is my code

import java.util.*;
import java.io.*;
class CheckRawTypeAdd
{
    public static void main(String[] sr)
    {   
        List<String> list_str = new ArrayList<String>();
        list_str.add("abcd");
        List<Integer> list_int = new ArrayList<Integer>();  
        List list_raw; 
        list_raw=list_str;
        list_int=list_raw;
        list_int.add(200);
        Object o1 = list_int.get(0);
        Object o2 = list_int.get(1);        
        System.out.println(o1);
        System.out.println(o2);
        //for(Object o : list_int)
        //{
        //  System.out.println("o value is"+o);
        //}
    }
}
Edit
Report