View Full Version : Main method in java and C
Abimanya
06-30-2005, 02:15 PM
This is a very basic doubt.
I know that in C and Java the program control starts from the main method..
But can anybody explain
1.why is that the main method is the starting of program
2. why is that the main method is given preference over any other methods??
3. Why is it named as main method?
4. How does the compiler knows that main method is the starting?
5. What is the compiler process(program) to identify uniquely the main method
suryad
06-30-2005, 08:27 PM
1. I guess the people who wrote the API for Java took some ideas from C and decided to stick with convention.
2. I dont understand what you mean by this.
3. It just makes sense...logical.
4. The compilers job is to know how to run the program hence it should know the starting point by standard is main.
5. I dont understand this as well. javac is the java compiler that comes in standard in sdk. When you type in "java Programname" it will throw an error unless it has a main method in there. Thats it.
tricolaire
07-01-2005, 05:01 AM
I know I'm totally letting my OCD get the best of me, but I need to point out that C has a main FUNCTION, not method.
Just wanted to point that out
suryad
07-01-2005, 10:42 AM
What is the difference between a function and a method? Arent they both the same thing?
tricolaire
07-01-2005, 01:36 PM
technically, a method is a function being stored in and run from a class. Since java is all contained inside classes, the main bit of the program is a main method. Since c isn't object oriented, it can't have classes, and thus can't have a method, hence the main function.
c++ has both functions and methods, but the main is a function.
suryad
07-01-2005, 07:51 PM
Great explanation thanks tricolaire. Its just in all classes I have taken, profs and so on have used 'methods' and 'functions' interchangeably in their vocabulary...
oracleguy
07-01-2005, 08:54 PM
Great explanation thanks tricolaire. Its just in all classes I have taken, profs and so on have used 'methods' and 'functions' interchangeably in their vocabulary...
People tend to do that but as explain before, they are technically different things.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.