The method signature is a combination of the name of the method and the parameters it takes. As such, two methods with the same name but different parameters are different -- indeed, this is what is called overloading a method.
The JVM knows the signature of the main method to look for and run automatically. Allowing for a no-parameter version of main would mean the JVM had two things to look for -- and what happens if you include both the parameter-less main and the one with the String[]?
I guess you would call one for no parameters and one for some, but it seems more elegant (to me at least) to have a single method and pass an empty array if necessary.
In short, it was a design decision. You don't actually have to do anything the the String array, apart from putting it in the method definition.
It's not mandatory, look
public static void main() {
// do stuff
}
Perfectly legal. Of course, the JVM will moan it can't find a main method, but that's another matter
The real answer is, the JLS (yes, the JLS - have a look) says a JVM will kick off proceedings by locating the method public static void main(String[]) and invoking it. As for the "And one more thing it optional in C or C++" comment, who cares? Java is not, in any way, either C or C++. Just because the syntax is similar, doesn't mean you should expect everything else to be the same!
Your answer is quite appreciable. Hope u r familiar with C or C++. In those languages main parameter should be optional. Then whatever u told that main has double declaration is that true for C or C++. I am very uncomfortable to pass such an argument which has no use indeed.
Thank u for ur reply.
> I know Java is not C or C++. But if something is
> possible in C or C++ and it not possible in Java
> without any proper reason that should be very unfare.
> I just want to know the reason behind that. Nothing
> more than that.
>
> Thank U
Why is it unfair? What would be the point of so many languages existing, only to be identical in the name of some perceived fairness? Java is not C or C++, there is absolutely no reason on earth why it should behave similarly to either language, none whatsoever. Fairness? To whom? The JLS mandates an entry point, because the designers wanted it that way. That is an end of it!
And what's "possible" and "not possible" here? You're not obliged to pass in or manipulate any parameters in main(), only to allow for the possibility.
> I know Java is not C or C++. But if something is
> possible in C or C++ and it not possible in Java
> without any proper reason that should be very unfare.
Don't be childish. There is a reason for it in Java. It keeps things simple.
> I just want to know the reason behind that. Nothing
> more than that.
You were already told.
> > But if something is possible in C or C++ and it
> not possible in Java
> > without any proper reason that should be very
> unfare.
>
> It's a design decision. Get over it.
>
> ~
But Mommy! It's not FAIR! How come C can have it and Java can't? WAAAHHHH!!!!
> Is the argv parameter optional in ANSI C?!
As a guess - yes.
Probably because it doesn't change anything at all. There will still be values on the stack. The only difference is that the code won't be able to get to them the normal way. Although there is probably a library call that will provide access.