Wednesday, March 22, 2006

Bug in asList method of class Arrays - Java Tiger: Reply

Not a bug, but I agree it's very surprising. Look at the declaration of asList():

public static <T> List<T> asList(T... a)

"T", like all generic type parameters, has to be a reference type. If you pass an array of reference types, then T becomes the type of that reference, and the individual array elements become the arguments to the function.

But if you pass a primitive array to this function, "T" is assigned the primitive array type itself -- int[], in your example -- and the function sees a single argument of that type. It has to be so, because T can't be assigned to type "int". That "1" is therefore indicating that the function saw the array as a single unit.

No comments: