HELLO WORLD PROGRAM EXPLANATION

SEE the simple program
class Demo{
//program always begins with main()
public static void main(String[] bhabani){
System.out.println("hello world");
}}
EXPLANATION:
-every program in java must have a class. The methods and variables must be inside the class.
-Demo is an identifier that is the name of the class.
// -This is the comment. JVM ignores the line
-Every program execution begins with the main(). Its take an argument array of strings.
The main method is Public because it is called by JVM which exist outside the class.
If you did not specify public then it is compilation error.
-A program can have two main().
like public static void main(String[] args) and void main(int x){}
The another main() is just like normal methods.
-main() is static because main() is independent of any instance of the class. It is executed before any instance present.The program execution starts from main().
All the console based programs has a main(). Applet do not have a min().
-return type of main() is void because it returns nothing.
-system is the pre defined class that provides access to the system. out is the output stream that is connnected to the console and println() is a predefined method. If main() is declared as private --- The program compiles properly but at runtime it will give “Main method not public.” message. What if the static modifier is removed from the signature of the main method? or do not provide the String array as the argument to the method? Result for above : Program compiles. But at runtime throws an error “NoSuchMethodError”.

No comments:

Post a Comment