Reputation: 267067
Since its possibly one of the most widely used methods of the Java language, why does it have to accept an array of Strings and doesn't work without it? For example, I could always live with:
public static void main() {}
over
public static void main(String[] args) {}
Is there a higher purpose to this than just being able to accept command-line arguments, especially since a vast majority of Java programs are GUI driven and don't need to receive args through command line?
Upvotes: 4
Views: 11612
Reputation: 115
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. For example, we can pass username, password from the command line itself.
class Test {
static void connectDB(String username, String password) {
System.out.print(String.format("Username: %s, Password: %s", username, password));
// code to connect DB
}
public static void main(String args[]) {
connectDB(args[0], args[1]);
}
}
Now compile the program using javac Test.java
and Execute using java Test rootuser Test1234
The output will be:
Username: rootuser, Password: Test1234
So, we can use commandline-argument this kind of purposes.
Upvotes: 0
Reputation: 5798
even a gui driven java app will start with some main method. The "higher purpose" has never been to accept command line arguments.
The purpose is just to accept arguments. Period. Whenever you start any program not just Java you will always need some syntax to pass arguments
Upvotes: 8
Reputation: 6395
Programs always take commandline arguments. It's up to the programmer to decide if they are used for anything.
So implementing a main without a string-array would lead to more complex startup-logic and potentially confusing and errorneous behavior, for the more than debatable gain of not writing a single parameter declaration less (in your whole program!)
And given the overall verbosity of Java & the support for common templates for boilerplate code in IDEs like Eclipse, I fail to see where that's really an issue.
Upvotes: 7
Reputation: 10809
I guess it's because many Java programs will take at least some parameters. Like C (a syntactical inspiration of Java if nothing else), Java considers the best way to provide these as parameters to main
.
Even a GUI program will often take command line parameters. Just because the shell it's launched from typically doesn't ask for these parameters (by default), doesn't mean they're not supported.
Upvotes: 2
Reputation: 1500275
I agree that it could be more flexible. In C# for instance, all of these are acceptable entry points:
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
The versions returning int
use the return value as the exit code for the process. If you don't care about receiving any arguments, you can use one of the versions which doesn't take any. Any arguments given when the program is launched are ignored.
So yes, it could be more flexible - but it's never struck me as a significant problem in Java.
Upvotes: 12
Reputation: 116334
there are also many java programs that receive args through command line. Think of javac, ant, maven, etc
Upvotes: 1
Reputation: 19810
Lots of GUI programs provide facilities to accept command-line switches to control their behaviour at start up.
Upvotes: 2
Reputation: 421978
What "vast-majority" of programs do does not really make much difference in terms of what is needed to be done. There are still lots of command line programs that are driven by command line args.
Upvotes: 2