Saturday, April 11, 2009

"Autoboxing" Array is Illegal

Wrote some array methods today then I had a question about auto-converting an int[] to Integer[] (auto-boxing) while I was driving back home.

For instance, this works,
public static  V wrapper(V v, Class clazz){
return v;
}

int i = wrapper(1, Integer.class);

But this is not compilable,
public static  V[] wrapperArray(V[] v, Class clazz){
return v;
}

int[] i2 = wrapperArray(i2, int.class);

int.class will be auto-converted to Integer.class but int[] will not. I can't find the exact line in JLS describing this, do leave a comment if you could.

No comments: