Reputation: 319
It is my first day to learn java, when we write a class (the very first one). Someone defines a main function someone defines a run function.
Are they the same?
Upvotes: 2
Views: 2875
Reputation: 2083
Lets suppose you have simple java application:
public class SimpleJavaApp {
public static void main(String[] args) {
System.out.println("HELLO WORLD");
}
}
When you compile it from console you type:
javac SimpleJavaApp.java
Next you run it:
java SimpleAppJava
When you run it java will run the "main" method for you. The main method is sort of an start (entry point) for your simple application. The method takes array of string arguments which you can pass to application during execution for instance:
java SimpleJavaApp someArgument
then within application
public class SimpleJavaApp {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
would print you:
someArgument
Next the run method is concept in java concurrency. run method is a start point for every new thread, though you should not worry about these for a while!
Upvotes: 0
Reputation: 1283
main
method is entry point of a program but run
method of Runnable
interface is entry point of a thread. JVM looks for a main method, so in order to launch a java program directly from JVM you must have a main method.
Followings do the same job but in different ways.
With just main:
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello from main!");
}
}
With calling a thread from main:
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
Examples from docs.oracle
Upvotes: 0
Reputation: 12993
The main
method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public
and static
can be written in either order (public static
or static public
), but the convention is to use public static
as shown above. You can name the argument anything you want, but most programmers choose args
or argv
.
Someone defines a main function someone defines a run function. Are they the same?
NO. You will learn run()
method in Thread
chapter. run()
is the entry point for the thread execution.
Upvotes: 0
Reputation: 76905
No, they are not the same. Also, in Java there are no functions. Use the term method
instead.
If the name of two methods differ, then they are not the same.
Upvotes: 0
Reputation: 5448
The public static void main(String[] args)
method is an optional method that you can implement within your class if you want to execute it as an external Java program. The main
static method will be called and an array of String
arguments will be passed to it.
For now this no run
method to worry about. You will encounter this again at a later stage when learning multi-threading. As said by @PaulDraper in the comments, any other static method that your define in your class can be invoked from main
, esp. if you want to keep the latter succinct.
Upvotes: 1