Generics have unexpected behaior using primitives array instead of wrapper array

The following code is returning a List of Integer:

Integer[] arr = new Integer[] {3,2,1};
List<Integer> list = Arrays.asList(arr);

Why is the same code using "int" returning a List of int[]:

int[] arr = new int[] {3,2,1};
List<int[]> list = Arrays.asList(arr);
Jon Skeet
people
quotationmark

Yes, it's treating that as a call using varargs and int[] as the type argument, i.e.

List<int[]> list = Arrays.<int[]>asList(new int[][] { arr });

The alternative would be to infer T=int... which is impossible, as Java generics don't support the use of primitive as type arguments. That restriction is the reason for the difference here.

people

See more on this question at Stackoverflow