2

As a Java rookie, I understand that the values that I pass to a method are called the arguments and the variables that receive the values in the method definition are called parameters.

Now, where ever I see a Java class written with main method, the method signature says

public static void main(String args[]) {}

For eg. http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Though I can give any variable names, wasn't it justifying if they had given params[] instead of args[]?

Any theories?

Thomas
  • 372
  • 2
  • 10

6 Answers6

5

There's no reason you couldn't call it whatever you wanted, honestly. It's just convention to call it args. I wouldn't recommend calling it anything but args, because that is what virtually every Java programmer expects. But there's nothing that says you couldn't rename it.

class BadArgs {
    public static void main(String params[]) {
        System.out.println(params[0]);
    }
}

C:\Documents and Settings\glowcoder\My Documents>javac BadArgs.java

C:\Documents and Settings\glowcoder\My Documents>java BadArgs Hello!
Hello!
corsiKa
  • 81,495
  • 25
  • 153
  • 204
4

The name is appopriate. args is a single parameter whose value is an array of command line arguments.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

Firstly, this is a fairly canonical name in many languages. e.g. in C, you normally see main defined as:

int main(int argc, char *argv[]);

(where argc is the count, and argv is an array of pointers to each argument). Java mimics this.

I can only guess as to why the canonical name is args and not params. Perhaps it's because if you want to know e.g. the 0-th arg, you refer to args[0]?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

The values passed to a function are arguments. The named variables that contain those arguments are called parameters. So it would be quite valid to name the parameters arg1, arg2 in every method if you wanted to be vague!

The answers to this question may make it clearer (or not):

What's the difference between an argument and a parameter?

Community
  • 1
  • 1
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
1

It could really go either way, args is pretty much the same thing as params, i guess they call it args because its command line "args" that are the parameters.

AJD
  • 11
  • 1
0

main() is a special method. and args[] is referring to the fact that the main function receives command-line arguments when the program starts from the user of the program.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58