Why the following code throws ArrayStoreException on line 3:
int[] ar = {2, 4};
List list = Arrays.asList(ar);
list.set(0, 3);
Unboxing should be performed here from Integer to int but it doesn't.
Why the following code throws ArrayStoreException on line 3:
int[] ar = {2, 4};
List list = Arrays.asList(ar);
list.set(0, 3);
Unboxing should be performed here from Integer to int but it doesn't.
You're assuming that Arrays.asList(ar) creates a List<Integer>, but that's wrong.
Arrays.asList(int[]) creates a List<int[]>, and setting an element of type int in that array explains the ArrayStoreException (saving an int in an int[] array).
If you want a List<Integer> with the same code, declare ar as Integer[]. This is probably another lesson to avoid using raw types (because the compiler would have prevented this runtime exception, had List<Integer> been used as the data type of list)