1

I'm a beginner in java.When executing a simple program I noticed that in main method

public static void main(String args[])

args[] can be given any name and it executes successfully. Why is that?

  • 1
    Because, it's just variable name. – Ravi Jul 03 '17 at 15:04
  • maybe because it's only a variable name – azro Jul 03 '17 at 15:04
  • 5
    The argument names are internal to that function, they don't have any consequence to anyone calling the function. – apokryfos Jul 03 '17 at 15:04
  • 3
    The name of the variable is for the programmer - not the program. The program works on a memory address. The programmer works on a name that refers to a memory address. Good question !!! – Dakoda Jul 03 '17 at 15:05
  • name's parameters are aliases, you can give to them any valid variable name – ΦXocę 웃 Пepeúpa ツ Jul 03 '17 at 15:07
  • If the compiler designers decided it was best for main to have fixed parameters (including names), then they would have applied the same logic to ALL methods. The result would be developers adding their own code to assign the variables with fixed names to variables that were more indicative of what they want their code to accomplish. This would have induced the compiler designers to optimize the assignment statements. So instead of beating around the bush, they did the right thing in the first place. – Jeff Holt Jul 03 '17 at 15:13
  • 1
    Why would there be a restriction on the variable name? – Elliott Frisch Jul 03 '17 at 15:13
  • @ElliottFrisch Some restrictions are reasonable, as I'm sure you realize. The type of restriction the OP mentions, if it were to exist, would be unreasonable. – Jeff Holt Jul 03 '17 at 15:22

5 Answers5

3

When you call a method, you don't care about what the names of its parameters are, do you?

Say I have this method:

public static void doStuff(int number) {}

And I can call it like this:

doStuff(10);

Do I need to use the parameter name number anywhere in the calling code? No.

Even if you call the method by reflection, (which I think is what actually happens when you run a java program) you don't need the parameter names.

Method m = clazz.getMethod("main", String[].class);
m.invoke(null, null);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

To put it bluntly: because the docs say so. 1

The main must be public, static, void and accept an array of Strings as it's parameter. The rest is up to the programmer... but there is not that much left other than the argument's name.

I can't speak for the many minds behind Java, but enforcing an argument's name is just not that important; the JVM doesn't care about a typo in args when it wants to invoke the main. What's important is the signature, which is defined by return type, name, and the type(s) of the argument(s) passed in.

Phil Kiener
  • 778
  • 1
  • 7
  • 15
1

The main method mus have a specific signature. The signature specifies method name and parameter types, but does not specify parameter names.

in other words, is must be public, static and void, be called main, and take an array of String as a parameter. It does not require a specific name for that parameter.

NickJ
  • 9,380
  • 9
  • 51
  • 74
0

args is just a variable name of the type String[]. It is like any other variable name that you have inside your program.

rowana
  • 718
  • 2
  • 8
  • 21
-2

as stated the variable name is only internal, the compiler does only care about the type (String array). for example if you call the method with reflection you can just pass a array of arguments object[] { new String[]{ "my arguments args[]" } ....

borehack
  • 87
  • 1
  • 5