This discussion is archived
2 Replies Latest reply: Dec 11, 2006 10:41 AM by 807607 RSS

varargs versus array as Arrays.asList param

807607 Newbie
Currently Being Moderated
Arrays.asList is declared thus:
public static <T> List<T> asList(T... a)
Why would it be written that way rather than
public static <T> List<T> asList(T[] a)
?
Are they not equivalent? If they are, wouldn't the second be more clear to most consumers of the javadoc?

Just checking whether there's something I don't understand...
  • 1. Re: varargs versus array as Arrays.asList param
    807607 Newbie
    Currently Being Moderated
    As declared asList can take an array or a list* of T's. Your alternative
    would limit the caller to using an array only.

    (http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html)

    * [Edit] I mean an argument list, not a List list...
  • 2. Re: varargs versus array as Arrays.asList param
    807607 Newbie
    Currently Being Moderated
    As declared asList can take an array or a list* of T's. Your alternative
    would limit the caller to using an array only.

    (http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html)

    * [Edit] I mean an argument list, not a List list...
    OK, got it. I misinterpreted p. 69 of "Java in a Nutshell" which says, "Varargs methods are handled purely by the compiler. To the Java interpreter, the max() method is indistinguishable from [one declared with explicit array syntax for the last argument]. To convert a varargs signature to the 'real' signature, simply replace ... with []."

    Now I see that indistinguishable to the interpreter is not the same as indistinguishable to the compiler.