-->

Could you technically call the string[] anything i

2020-02-07 10:22发布

问题:

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?

回答1:

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)


回答2:

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)


回答3:

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