Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

What does (String [] args) do?

+10 votes
asked Oct 5, 2016 in CSC 211 (F16) by Ryan Philp (702 points)

1 Answer

+2 votes
  1. 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.

  2. Cleanness of design (aka the DRY principle). Don't specify two entry point signatures when one can do the job. And clearly, it can.

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

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

None of this means that it would have been unambiguously wrong to do it another way. And for example C# gives you alternative signatures, and deals with the ambiguity problem by requiring the developer to designate an entry point some other way. -Stephen C on stackoverflow.com/questions/10783190/why-does-main-method-in-java-always-need-arguments

answered Oct 5, 2016 by Dylan Hart (1,360 points)