RSS

Writing your First Hello World Program in JAVA

06 Sep

Before attempting to start programming in Java make sure that you have Java installed in your computer.
This example is simple and easy to understand. The program will print “Hello World” in the console.
To start open notepad and type the following code:


/**
 * This is a public class named HelloWorld.
 */
public class HelloWorld {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Hello World!!");
	}

}

1. Save this file by naming it HelloWorld.java. Make sure that the file name and the class name are same.
2. Open the command console and go to the parent directory where you have saved the HelloWorld.java file. Give the following command: javac HelloWorld.java and press Enter key. This will compile the HelloWorld file and make a class file of it. This file has the bytecode which will be executed by the JVM.

3. After that you can run your java program by running the following command:
C:\>java HelloWorld

This will give you output of “Hello World” in the console.
Source Code Explanation

You have complied and have seen your program run successfully. This all is good but it is also very important to understand how it works. The source code has three main components:

  • Comments
  • Class Declaration
  • Main method

Comments: The comments start from “/*” and end by “*/”. They are ignored by the compiler and are only useful for the programmers to improve readability of the code.  Java supports three types of comment types:

/* text */ Compiler completely ignores whatever comes between.

/** documentation */ These comments are called documentation comments and are also ignored by the compiler. The javadoc tool uses these comments to make automatically generated documentation.
// text: The compiler ignores everything from // to the end of the line.

Class Declaration: The following code declares a public class whose name is HelloWorld.

public class HelloWorld {
…………….
….
}

The class declaration uses the keyword class for its declaration and the code is written in between the opening and closing curly braces.

The main Method: The following code shows the main method. This method accepts a single argument: an array of elements of type String. Every Java Application must have this main method to run.  You can interchange the position of public and static keyword and doing this will not affect your code.

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

The code in this method is written between the curly braces.

The line System.out.println(“Hello World!!”); in this example uses System class to print the message “Hello World!!” to the console.

Advertisements
 
Leave a comment

Posted by on September 6, 2010 in Java

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: