Why String args[] is mandatory in main() ?

In public static void main(String args[]) why it is mandatory is carry String args[] while i am no intention to pass command line arguments. And one more thing it optional in C or C++.
[191 byte] By [iamegara] at [2007-11-27 8:16:14]
# 1

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.

glennjia at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 2

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!

georgemca at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

iamegara at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 4
Java is not C or C++ ......
ita6cgra at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 5
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
iamegara at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 6

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

georgemca at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 7
Bloody C-worshippers. FORTRAN has named common blocks. Why can't Java have them too?
DrClapa at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 8

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

jverda at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 9
> 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.~
yawmarka at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 10

> > 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!!!!

jverda at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 11
> But Mommy! It's not FAIR! It's unfare. That means you don't have to pay to use it.~
yawmarka at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 12
Is the argv parameter optional in ANSI C?!
Hippolytea at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...
# 13

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

jschella at 2007-7-12 20:01:07 > top of Java-index,Java Essentials,Java Programming...