可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 line agruments in Java are barely used anymore. And when I try and run this...
//this program won't compile
public class SomeClass {
public static void main(){
System.out.println("This text will never be displayed :(");
}
}
The output shows this in red text:
Error: Main method not found in class SomeClass, please define the main method as:
public static void main(String[] args)
I, the newbie Java programmer, would greatly appreciate it if anyone told my why it's required to enter that parameter into a main method.
回答1:
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?
回答2:
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.
回答3:
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! :)
回答4:
Because
- 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:
- 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.
回答5:
I actually have no idea why it's required, other than to say that it is a syntactical convention. In the same way that a function is (defined function-name()) in Lisp/Scheme, or there are do..end blocks in Ruby, the syntax of a Java Main function is with String[] args.
As for not using command line arguments, it's entirely dependent on the program. Entirely. I write programs in java all the time that take command line arguments; it's just a question of what you're trying to accomplish.
回答6:
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.
回答7:
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.