Thursday, June 27, 2013

Demystifying the main() function

We all know about the main() function or main() method- the entry point of execution of all the programs of most of the programming languages we use now. Starting from C to C++ to Java and C# and much more main() is utilized in all the programs to make the program work. Even before we came to know about what functions/methods are, we have started using it. With the advent of new languages specially object oriented ones main() has come a long way from int main() (in C) to public static void main(String[] args) (in Java). The function of main() is still same, to serve as entry point for execution, then why a such giant leap in the syntax? Lets find out the answers from the following to demystify main()...


Q1. Can we overload main() function/method?

Ans. The answer is YES as well as NO depending upon which language you are using. More often than not if the main function of a particular language doesn't accept parameters or arguments then it can't be overloaded like in C or C++. If you try to overload main() than the compiler will show you some errors. While in cases where main() accepts parameters usually a String array, it can be overloaded like in pure object oriented languages ( C# and Java ). 

Q2. If main() can be overloaded then how the compiler understand which is the entry point for execution?

Ans. The compiler will consider the main() method with a single argument of string array as entry point for execution i.e static void main(String[] args) will be the entry point for compiler. If you are overloading main() and not using static void main(String[] args) than the compiler wont allow you to run the program in Java. The answer is in context with Java.

Q3. Why the main() has to be static?

Ans. Before answering the question lets revise what the major characteristic of any static method is? Well the obvious answer is that such methods don't need any instance of class when called. Plus they are loaded into the memory even before the class is loaded. Since the main() is entry point for execution, it has to be the first function or method in the memory and if it is not declared static, it will require an instance that will ultimately load the class in the memory before main() is loaded. Which is quite contradictory to the fact that main() has to be the first one utilizing memory since it is the entry point for execution.

Q4. Why main() accepts a String array (String[] args or String args[])?

Ans. The answer lies in the statement that in object oriented languages main() requires some command-line arguments when the program is made to run. These can be passed as an array of Strings. That is why it accepts a String array. The other reason behind this signature of main method  is that whenever main is overloaded, compiler must know which is the default entry point among other main methods and one with single argument of String array is considered as the default entry point for execution by the compiler.

Q5. Why main() method is public in Java?

Ans. Any method which need to be accessed outside the class must be public. main() method in Java has to be public so that it can be accessed by the JVM for the execution.

Q6. Why public constraint of Main() in C# is not necessary as in case of main() in Java?

Ans. Main() has to be public since it needs to be accessed outside the class by the machine which will run the program. In case of C#, even if the Main() is not declared public, .NET runtime is designed in such a way that whenever Main() is invoked, the compiler is able to bypass the access visibility of the Main function.


Note: In C# main() method is written with capital 'M' as Main(), while in most of the other languages, it is written with lowercase m as main().



No comments:

Post a Comment