Wednesday, 4 January 2012

First Java Program


/* First Java Program */

1.       Print Hello World using Java Program.

class HelloWorld
{
                Public static void main( String args[] )
                {
                                System.out.println( " Hello World..!!! ");
                }
}

-          Save the file with HelloWorld.java in any folder. ( You can use any name for the file, but need to execute with class name only ).
-          Open cmd. ( Goto Run, type cmd)
-          Compile & Execute the application as follows:


Javac is used to compile the application.
Java is used to execute the application.

Program Explanation:
*       In Java, Entry point is main().
*       Hence JVM first calls main(). To understand the java architecture, refers to above article.
*       We define the class HelloWorld inorder to encapsulate main method.
*       Here, class = Keyword and HelloWorld = identifier.
*       Every class name should be Uppercased, if it is concate nated, each word must start with uppercase.
Ex1: Hello
Ex2: HelloWorld

                Description about main():
*       Public:
Main is defined as public because to give permission to JVM to call it.
*       Static:
Main is defined as static because the JVM can call it without object creation.
*       Void:
It is declared as void because, JVM doesn't need any response back from main().
*       String args[]:
Main contains args[] as parameter because it provides the facility to input variables from command prompt.

                Compiling and Execution:




No comments:

Post a Comment