4

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
  • Maybe you ignore any arguments but what about the people that wish to support arguments - typically from the command line ? Command lines etc typically pass arguments via a Steing array. If you don't care about them - ignore them. Adding support fir main() just adds ambiguity in cases where both main with and without arts are present. Commented Apr 17, 2009 at 8:25

9 Answers 9

12

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.

Sign up to request clarification or add additional context in comments.

5 Comments

which of the above will be used when you provide all 4 Main methods ?
If you tried to give all it would probably return a syntax error.
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.
@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.
In Java you could provide all four versions, though it does require extends another class (other than Object).
8

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

3 Comments

That answers a part of my question, but what are those arguments used for?
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.
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.
7

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.

Comments

3

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

Yes: legacy.

Comments

2

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.

2 Comments

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.
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
2

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

1 Comment

to add to your statement, many GUI programs accept "file names" as arguments. (think of an MSWord document dragged into the executable).
2

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.

Comments

1

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.