My first java program
Class Hello
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Setup to compile and run your first java program
Step 1. Save the file as Hello.java
Step 2.Open command prompt and go to the directory where you have saved the first java program as Hello.java ,assuming it is C:\
Step 3.Type javac Hello.java and press return to compile your code.If there is no error in the code, the command prompt will take you on next line of the window.
Step 4.Now type java Hello to run your program.
Step 5.You will able to see Hello World printed on the command prompt.
Importance of public static void main(String args[])
whether a class contains main( ) or not and whether main( ) is declared according to the requirements ,these all things are not be checked by the compiler,these are checked by jvm at Runtime .
Note: If JVM is unable to find main( ) then we will get a Runtime Exception saying : Exception in thread "main" java.lang.NoSuchMethodError: main
public: jvm calls main() from anywhere i.e (with in class,outside the class,outside the package etc) so it is public
static: without creating object also jvm has to call this method
void: here main( ) won't return anything to JVM
main( ): this is the method which is already configured in jvm
String[] args: commandline- arguments in the form of array of instances of the class String
There are several changes can be made in prototype of main() method:
1.The order of access modifier is not important ,we can use both public static or static public
2.We can declare String array in any acceptable form
String args[ ]
String[ ] args
String [ ]args
3.We can replace String args[ ] with the variable paremeter
public static void main(String...args) 3 dots represents variable parameter
4.Instead of argument we can use any variable(valid identifier)
public static void main(String viku[ ])
5.We can declare the main( ) method with the following modifiers also
final
synchronized
strictfp
i.e final synchronized strictfp staic void main(String args[ ])
No comments:
Post a Comment