Why doesn't Java's main use a variable length argument list?

froadie picture froadie · Feb 4, 2010 · Viewed 7.1k times · Source

I have a question about the syntax of the Java main declaration:

public static void main (String[] args)

Since you can pass a variable number of Strings when invoking the main function, shouldn't this be a variable length argument list rather than an array? Why would a command-line invocation of this method with a list of string parameters even work? (Unless there is behind-the-scenes processing that builds an array with the list of strings and then passes that array to the main method...?) Shouldn't the main declaration be something more like this...? -

public static void main(String... args) 

Answer

Henning picture Henning · Feb 4, 2010

main(String... args) and main (String[] args) are effectively the same thing: What you're getting is a String array. The varargs is just syntactic sugar for the caller.

I guess as you never call main() from code, it wasn't retrofitted when varargs were introduced.

Edit: Actually, scratch that last sentence. main(String... args) is perfectly valid syntax, of course. The two styles are completely interchangeable. This works just fine:

public class Test {

    public static void main(String... args) {
        System.out.println("Hello World");
    }

}