views:

1702

answers:

8

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?

+1  A: 

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.

Mehrdad Afshari
That only means that you *should* be able to receive arguments when you need them. You don't have to be *forced* to receive them if you don't need them though. That was a choice on the part of the language designers.
Jon Skeet
Your statement is true. In this specific case, I'm with Java language designers (I'm a C# fanboy). It doesn't really harm to have an unused argument but it promotes consistency in the language. Anyway, it's a very minor issue and definitely a choice made by the lang designers; Not much to talk about
Mehrdad Afshari
+3  A: 

"Is there a higher purpose to this (..)"

Yes: legacy.

boj
+2  A: 

Lots of GUI programs provide facilities to accept command-line switches to control their behaviour at start up.

dommer
to add to your statement, many GUI programs accept "file names" as arguments. (think of an MSWord document dragged into the executable).
Mehrdad Afshari
+8  A: 

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

Peter
That answers a part of my question, but what are those arguments used for?
Click Upvote
The are used to do whatever they are designed to do.They can be a filename to be used to displayed in some GUi tool or the numbers you want to generate a puzzle with.
Peter
This design choice was guided by the convention of command line arguments. The similarity to the standard entry point for C programs is not coincidental.
erickson
+1  A: 

there are also many java programs that receive args through command line. Think of javac, ant, maven, etc

dfa
+8  A: 

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.

Jon Skeet
which of the above will be used when you provide all 4 Main methods ?
Peter
If you tried to give all it would probably return a syntax error.
Click Upvote
Well, you couldn't give all 4 anyway because you can't overload by return type. But if you provide two it results in an error CS0017.
Jon Skeet
@Peter: Since Main is special anyway, the language could specify any other behavior - like calling the second version if and only if OS passed actual arguments.
MSalters
In Java you could provide all four versions, though it does require extends another class (other than Object).
Tom Hawtin - tackline
+2  A: 

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.

Edmund
+4  A: 

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.

deets