hello world program will be a very simple program that will print
Hello,World! to console.

Before running the program, you need to make sure java is properly installed on your machine.

Prerequisite for running a java program

Install the JDK if you don’t have installed it, download the JDK and install it.

set path of the jdk/bin directory. you can follow this link for setting the path

You can either write a program in IDE such eclipse or you can simply write in in text editor.

1

2

3

4

5

6

7

8

// First java hello world program

publicclassHelloWorld{

publicstaticvoidmain(String[]args){

System.out.println("Hello, World!");

}

}

You need to save this file as
HelloWorld.java.

How this java program works

Let’s see detailed explanation of Hello world program.

Class declaration

1

2

3

// First java hello world program

This is a comment in java and this statement will be ignored at run time.

Java

1

2

3

publicclassHelloWorld{

As java is object oriented programming, every java application should have class definition.

Class declaration: A class declaration includes name and visibility of the class.
classHelloWorld is declaration of class which includes keyword
class, followed by identifier
HelloWorld

Class declaration is followed by curly braces
{}, which defines class’s definition.

Main method

1

2

3

publicstaticvoidmain(String[]args){

This is called main method in java. This is entry point for this program.

public: This is access modifier which is used to define visibility. Here main method will be visible to all other classes.static: static members belongs to class rather than any specific object. It simply means you can access the static members without object.void: void is another keyword which defines return type of main method. It simply means main method does not return anything brefore terminating the program.main: main is method name here.Stringargs[]: You can pass arguments to java program using args[] array. It is used to take user input using command line.

Print statement

1

2

3

System.out.println("Hello, World!")

System.out.println is used to print
literals in double quotes(
"") to console. As we have passed “Hello, World!” here, it will print
Hello,World! to console.

Semicolon

As you can see, each statement is terminated with a
semicolon(;). You can put new lines or spaces in the code but statement has to be ended by semicolon.

Compile and run the program

If you run this program in eclipse ide, you can simply right click and on
run asjava application.
You can compile this java program using command line as below:
Open the command prompt and go to the location where you have saved
HelloWorld.java

$ javac HelloWorld.java

You can run the program using command line as below:

$ java HelloWorld

When you run above program, you will get below output:

Hello, World!

When we execute java program, we need to give full classname without
.java extension.

Exercise

You need to print
Yeah!!Iexecuted my first java program on console and name of the class should be
MyFirstJavaProgram