What's the point of String[] args in Java?

前端 未结 7 1783
感情败类
感情败类 2021-01-17 06:50

Whenever you declare the main method in a class, you always have to do a String array called \"args\". What\'s the point? Unless I live under a rock, command li

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 06:53

    Because

    1. Command-line arguments are still used, even by many UI programs (did you know that Microsoft Outlook supports some very handy command-line arguments?)*; and:
    2. That's Just How Java Works (TM). Among other things, it reduces the complexity of both the code (by disallowing multiple forms and possible accidental shadowing) and run-time (by not needing to find out "which main" to call). Allowing a secondary form without "args" just adds too little.

    Happy coding...


    *Yes, Outlook is not Java. However, if Outlook has command-line arguments, well, they must still be worth something -- it was a hyperbole ;-)

    Almost every UI program that deals opening reading files will allow specifying which file to open via command-line arguments (Gimp, Notepad, Firefox, to name a few others). Among other things, this is to allow integration with "double clicking to open" on items in Windows Explorer and similar.

提交回复
热议问题