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

前端 未结 7 1780
感情败类
感情败类 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:51

    A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. (From Command-Line Arguments) and as everyone else said here, it is the way it is!

    For gods sake,Please Don't say if I don't need this ,no-one else need this! :)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-17 06:59

    One case I can think of is, when you want to have a command line driven interface for your software along with the GUI. An example is the Android tools, all of them have console driven interfaces.

    0 讨论(0)
  • 2021-01-17 07:07

    Short answer: because that's the way Java is.

    Command-line arguments are used all the time, but you don't always see them due to launcher scripts, or because the program's running on a server, etc.

    That said, a lot of time the command line arguments are of the -D variety, slurped up by the JVM before reaching main. But it depends on what you're doing.

    0 讨论(0)
  • 2021-01-17 07:10

    Because that is the signature of the main method that is called when you execute a Java class. There needs to be some convention which method will be executed. By convention it is the

    public static void main(String[] args) method
    

    And yes, you do live under the rock, there are plenty of situations when command line arguments are used. Why would they not be used?

    You could ask: why require it? Why not just pick any other main method? The answer is that it would be adding complexity with 0 benefit. As is now, main function looks distinctive. If you look at it, you know it is the one that will get called. If any main would be called, you would have to always ask yourself: is the main I am looking at the one to be invoked, or is there another main in this class which takes precedence?

    0 讨论(0)
  • 2021-01-17 07:12

    Command-line argument support is largely standard among programming languages. Even in this age of GUIs, there are all sorts of hidden ways to run a program with command line arguments. I know Windows has shortcut configurations for advanced users where you can run a program with a given set of command line arguments, for example.

    Java also enforces types, and by extension function signatures (look it up on Google if you don't know what those are). The main function is expected to take an array of Strings - if the main function you define doesn't match that argument signature (1 argument, array of Strings), then it causes an incompatibility.

    Java supports function overloading (you can define the same function name several times with different arguments). To find which function to invoke, Java takes the input argument types and looks for an applicable defined function that takes the matching arguments.

    When the program runs, Java specifically looks for a function named main with 1 argument (String []). Your program doesn't define a main function with that argument specification, so this lookup fails with an error message.

    0 讨论(0)
提交回复
热议问题