answersLogoWhite

0


Best Answer

// C

int main(int argc, char** argv) {

return 0;

}

// Java

class MainClass {

public static void main(String[] args) {

}

}

Differences:

* Java main method must be in a class; C has no class, so no such requirement exists. * Both accept an array of command-line args. * ** Java arrays know how long they are; C does not and thus the main function must have an additional parameter to know the length of the array. * The Java main method has a specific format; the C main function seems to be allowed to differ. * ** The C main function seems to be able to accept different numbers of arguments and have different return types (assuming the implementation allows it).

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

The main in Java is the entry point of the program. A Java program starts from main. main is a function. Java is an object oriented language. It means that everything in Java can be called only through objects. But how does a program start? something must be able to create an object isn't it? for this there is main function. Notice the declaration of a main function it will be as :-

public static void main(String[] args) {

}

Here public indicates that the function that you call can be called anywhere from the program(out side the declared classes). static is a keyword which tells that you can directly call a function (in this case main) without creating an object for the function. void tell that the function main doesn't have a return type. In effect public static void main() {}, act as the entry point of the java program.

Remember there will be only one main function in a Java class. Then objects are created inside the main function inside the Java program like

public class Test {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.myFunction();

}

} // end of Test class

class MyClass {

public void myFunction() {

System.out.println("I am from MyClass");

}

} // end of MyClass

The output of the program would be

I am from MyClass

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

void main() function just declares a function with return type void..but in java during execution of the program to call the members of a class before an object is created we declare the members as static..so main function must always be declared as static so that it must be called before object is created..and it is declared as public because it can be accesed by code outside the program...main function will not be executed if it is not declared as static in java..

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Ideally, a main function in both C and C++ should explicitly return an int to the caller, but in C++ an int with value zero is implicitly returned anyway. And while a main function that returns void is considered an invalid program in C++, this rule isn't strictly enforced in C. Other than that, there is no practical difference.

Generally, a main function in both C and C++ should accept command line arguments and should return an int. The following is a typical signature:

int main(int argc, char* argv)

{

// ...process arguments

return( 0 );

}

The char pointer type may be replaced with a wide-char equivalent (such as wchar_t or unsigned short), depending on whether UNICODE is defined or not, or you may use a TCHAR macro equivalent if available. The first argument, argv[0], is always the fully-qualified path and file name to the program itself, which allows the program to refer to itself even if the user renames the executable. All other arguments in the range argv[1] to argv[argc-1] are those supplied by the caller.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

They are the same thing, but in C++, you don't have to use "return 0;" at the end. This feature makes C++ much eaiser to use.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between main function in C java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


Who invoke main function in java?

The Java Runtime Environment invokes main methods.


In java public static voidmain function denotes what?

Java's main function denotes the entry point into the execution of your program.


What is the name of the function which must be defined in a Java program?

The main function. Every program must have a main function and it must be declared static.


What is a main function in java?

the main function or main method is the first method that gets invoked when you try to run a java application. Any class that has a main method can be executed in a standalone fashion. A typical main method would look like below: public static void main(String[] args) { … // code goes here }

Related questions

What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


Who invoke main function in java?

The Java Runtime Environment invokes main methods.


Main difference between Array List and Vector in Java?

List is not sync'd as a vector is.


What is the main difference between java and dotnet?

java is from sun and .net is from microsoft, java is a language + runtime and .net is run frame that supports multiple language.


Is main a predefined function in java?

No. It is a user defined function which the person who is creating the java class has to code by himself.


What is the difference between function and method?

functions have independent existence means they are defined outside of the class e.g. in c main() is a function while methods do not have independent existence they are always defined inside class e.g. main() in Java is called method. ######## I've been studying OOP lately and had this question myself, so I will share my thoughts; I was taught that "A Function should do 1(one) thing and do it well." In specific Regards to PHP; The difference between a Method and a Function is that a Method is tied to a specific class. Hope this helps. -------------------------------------------------------------------------------------------------------- function can return value where as method can't that is the main difference between function and method --------------------------------------------------------------------------------------------------- Actually you are describing the difference between a function and a procedure. Function returns a value, procedure does not unless you are using c#, then everything is a function. In c# a function, to paraphrase the first answer, does and thing and does it well. A method contains functions. The most important method is the Main method. All functionality of a program must be referenced in the Main method because when you run a program, it starts at the beginning of the Main method, and stops wehn it hits end of the Main method.


In java public static voidmain function denotes what?

Java's main function denotes the entry point into the execution of your program.


Can we execute java program without main function?

no


What is the name of the function which must be defined in a Java program?

The main function. Every program must have a main function and it must be declared static.


What are the main differences between Java and C plus plus?

the difference is that c plus is better because you get big grades


Difference between ASP and JSP?

The main difference between ASP and JSP is that JSP is more script like and includes Java, whereas ASP does not. Both of these are server side languages.


Is the any class containing main function in a webapplicatin?

No. Java classes in web applications do not have Main Methods.