Why the main method in java is
having argument of only String[] . Why can't it be anything else (ex int[],
char[]) ?.
I could
think of below 5 reasons why String[] arguments and not any other parameters
1) It’s the JVM which calls
main() and JVM receives parameters as strings from I/O or frm OS, hence the
same signature. By default , JVM support only String.
2) Because that's the way it
was designed. Yea, I know that's a circular reason. But the point is that this
is the way it is and it ain't going to change. So unless you are planning on
designing your own language, the question is moot.
3) Cleanness of design (aka
the DRY principle). Don't specify two entry point signatures when one can do
the job. And clearly, it can.
A String[] can be
represented as Char, Int, Boolean which sounds fair but not vice-versa.
4) Semantic simplicity.
Suppose (hypothetically) that Java did support both void main(String[]) and
void main() entry points. What would happen if a class defined both methods? Is
that an error? If not, which one takes precedence when there is ambiguity? Is
this confusing yet?
By only allow void
main(String[]), the JLS avoids the problem.
5) This is analogous to the
standard C and C++ entrypoint signatures. (Admittedly, some C / C++ runtimes
support other non-standard entrypoints as well ... but that's not exactly a
good thing ... IMO.)
Feel free to comment below if you think of any other reason.
No comments:
Post a Comment