Why can a Double be added to a List of Integers using reflection

java

Solution

Generics are a compile-time thing. At runtime, a regular `ArrayList`, without any additional check, is used. Since you're bypassing the safety checks by using reflection to add elements to your list, nothing can prevent a `Double` from being stored inside your `List<Integer>`. Just like if you did

List<Integer> list = new ArrayList<Integer>();
List rawList = list;
rawList.add(new Double(2.5));

If you want your list to implement type checks at runtime, then use

List<Integer> checkedList = Collections.checkedList(list, Integer.class);

Problem

Why does this code run without any exceptions? ``` public static void main(String args[]) { List<Integer> a = new ArrayList<Integer>(); try { a.getClass() .getMethod("add", Object.class) .invoke(a, new Double(0.55555)); } catch (Exception e) { e.printStackTrace(); } System.out.println(a.get(0)); } ```

Original source

Related problems