0

The header for the main method is

public static void main (String[] args)

Could you technically replace "args" with anything you want? Also, why is the parameter an array?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user147219
  • 355
  • 1
  • 4
  • 13
  • 1
    *Could you technically replace "args" with anything you want?* Why don't you try? *Also, why is the parameter an array?* So you can pass multiple command-line arguments. – shmosel Mar 08 '17 at 19:32
  • You can rename it with any proper Java identifier. For instance you can use `foo` but you can't name it `1array` since variable names can't start with numbers. We call it `args` as short for "arguments" which are passed when we run this code via `java YourClassWithMainMethod arg0 arg1 arg2`. – Pshemo Mar 08 '17 at 19:37
  • Yes you can rename it to whatever you want. The type is an array because that is how it was implemented. Each element in the array contains a token from the command line. – toongeorges Mar 08 '17 at 19:38
  • The signature is based on the [`main` function of C programs](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean), whose declaration, generally speaking, is `int main(int argc, char *argv[])`. Java doesn’t need `argc` because its arrays have a length field. – VGR Mar 08 '17 at 21:51

3 Answers3

4

args is just a name for the argument in a method. You can rename it to whatever you want. The JVM doesn't need to know what the argument is named; only the type, String[], is important.

dont break the start point of the app

 static void main(String[] whateverYouNeed)
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    Just to add on to this and address the second part of the question, it's an array because you can pass multiple args (quoted or separated by whitespace) eg `java main somearg someotherarg "some third arg"` will result in your `String[]` param being `{"somearg","someotherarg","some third arg"}` – CollinD Mar 08 '17 at 19:34
1

You can rename it to any proper Java identifier. The application needs to be able to accept multiple command-line arguments. It doesn't necessarily have to be an array, you can use varargs also.

 static void main(String... whateverYouNeed)
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
0

The main method could be written as:

  • public static void main (String[] args)
  • public static void main (String args[])
  • public static void main (String... args)

The parameter name args could be replaced with any valid Java identifier like blah, programArgs, and so on. It's an array to accept any number of command line arguments. For example:

  • java programName has no command line arguments
  • java programName arg1 arg2 has 2 command line arguments
Joe
  • 320
  • 1
  • 7