Please see the code below.
import java.util.ArrayList;
import java.util.List;
public class Generics {
public static void append(List list) {
list.add("ABCD");
}
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
append(intList);
System.out.println(intList.get(0));
}
}
In list.add("ABCD"), it is expected that run-time exception will occur, since ABCD is not an Integer. I know that 'list' is of raw type, so list.add() won't give any compilation issues, but it should break while running the program, right?
And again - System.out.println(intList.get(0)) - 'intList' is the list of Integers, and it is expected to return integer value, but 'ABCD' is not an Integer. Why this is also working?
Can you please clear my doubts, why this code is working with out any issues?