Java Hello World Program


In this post, we will see how to write first java program. This post is intended only for java beginner.

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.

You need to save this file as

How this java program works

Let’s see detailed explanation of Hello world program.

Class declaration

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

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.class HelloWorld 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

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.
String args[]: You can pass arguments to java program using args[] array. It is used to take user input using command line.

Print statement

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.


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 as java 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

$ javac

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.


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

Important points

Let’s go through some important points about Hello world program.

  • Any java source file can have multiple classes but it can have only one public class.
  • Java source file name should be same as public class name.That’s why I said above “you need to save this file as”.
  • When you compile the java file, it is converted to byte code with .class extension, so you should be able to see HelloWorld.class in the directory.
  • When you execute the program, jvm looks for main method and will execute it. You need to be very careful with signature of main method, otherwise program may throw java.lang.NoSuchMethodError: main.

Add Comment