-2

Why does

" public static void main(String[] arg) { "

work instead of

"public static void main(String[] args) { " ?

Does it have anything to do with the String-Array?

And how exactly does it work? May using "arg" instead of "args" just be a tiny bit more efficient?

3
  • What do you mean with "Why does X work instead of Y"? Doesn't "Y" work for you?
    – Tom
    Dec 9, 2020 at 12:17
  • Why would "arg" be more "efficient" than "args"? If anything it is less efficient since the name now suggest that it is only one argument and no longer an array of arguments and that hinders readability, reducing programmer's efficiency.
    – luk2302
    Dec 9, 2020 at 12:18
  • 2
    You can name your method parameters whichever way you want - Java does not care at all. You only have to make sure that your entry point is public, static, void, called main and takes one parameter of type String[]. That being said, you can change it as much as you want as long as you still fulfil that. For example static public void main(String... foo) is still valid.
    – Zabuzard
    Dec 9, 2020 at 12:20

1 Answer 1

4

There is absolutely no difference, beyond the normal differences of parameter names (as in, you have to use the right name within the method body). The name of the parameter to the main method is entirely by convention. It's entirely valid to write:

public static void main(String[] dontUseJavaUtilDate)

as the declaration for your entry point.

From JLS 12.1.4 which uses Test

Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

There's no specification of the parameter name.

Ditto from the JVM specification section 5.2:

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

Not the answer you're looking for? Browse other questions tagged or ask your own question.