Question

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) 
Was it helpful?

Solution

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");
    }

}

OTHER TIPS

You can declare main either way, and it works just fine. There are no "backward compatibility" or "retrofitting" issues. However, readers of your code may find it distracting, and it is unlikely to improve your program in any way.

The Java Language Specification (third edition) section 8.4.1 says that "If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]".

The specification for how a Java program starts up is in JLS 12.2, which references chapter 5 of the VM spec. The VM spec section 5.2 says that the VM invokes a public class method "void main(String[])". Since the VM spec has no concept of variable arity, a main that was declared using "..." satisfies the requirement.

The main method was designed for Java 1.0.

The "..." syntax was introduced in Java 1.5

It's implemented via an array of the type you defined (my guess, but... if Java 1.4 and 1.5 byte codes are compatible so it must be implemented with arrays).

There is no Java's main method, in fact you can declare the array as a vararg in your main method:

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

The java main syntax predates varargs, which were only introduced in java 1.5.

Shouldn't the main declaration be something more like this...?

public static void main(String... args) 

Actually, that could be done without a problem.

It was String [] args before var args were introduced but nowadays both work.

Well, the 2nd syntax is just a variation of 1st and yes it would be nice but it's not built into Java at this point

Why do you care? Nobody calls the main method by hand. It is internal to Java.

EDIT : I guess it doesn't matter now as you can define main method as :

public static void main(String... args);

My point is still valid as no one calls main explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top