Could you technically call the string[] anything in the main method?

偶尔善良 提交于 2019-12-17 21:19:04

问题


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


来源:https://stackoverflow.com/questions/42680306/could-you-technically-call-the-string-anything-in-the-main-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!