Writing and executing simple 'Hello World' java program

The best way to learn Java is by directly writing programs. Here we are assuming that the reader is having at least the basic knowledge of C or C++ language.

Setting up Java Runtime Environment on your local machine -

Before we can write and execute any java program, we need to set up java runtime environment on our local machine. For that, we have to follow the following steps -
1- Download JDK -
To execute java programs, you need a compiler and interpreter, and they are provided by a software named Java Development Kit (JDK).
SO first of all you need to download the latest version of JDK from Oracle website and install it. You can download it from the following link -

2- Set path for JDK -
After installing the jdk you need to set the path for java. To set path follow the following steps -
i - Go to MyComputer and then open Local Disk (C). There will be a folder named Program Files.
ii - Open the folder and you will find another folder named Java.
iii - Open that folder and you will find a folder named jdk1.8.0_102 (the number following the jdk may vary depending upon the version of jdk you installed).
iv - Open that folder and you will find a folder named bin, after that just copy the complete address shown in address bar (that may look like this - C:\Program Files\Java\jdk1.8.0_102\bin)
v - Now right click on MyComputer and go to Properties there you will find a option named Advanced system setting, click on that.
vi - A windows will open, find a option named Environment Variables and click on that.
vii - A a new window will open click there on New option. It will open a new window named New User Variable.
viii - It contains two option variable name and variable path. Enter variable name as 'path' and in variable value paste the path that you have copied previously.

Hello World Program -

Open the Notepad and write the following program

 class Demo
   public static void main(String args[])
     System.out.println("Hello World");

Program Description -

  • The first line - class Demo - declare a class where class is a keyword which is used to declare a class in java. In java everything should be within the class. Demo is the name of class , it is user defined and it can be any valid java identifier.
  • The next line - { - is opening curly braces from where body of the class started.
  • Next line - public static void main(String args[]) - is the main method from where java program execution starts. Main() function contains a number of keywords -
    • public - public is an access specifier that causes the main function to be accessed everywhere. We will cover access specifiers in detail later. main() method is public because compiler has to access the main() function to start execution and compiler is outside the program so to make main() function accessible to compiler main() is declared as public.
    • static - static is another keyword used in java, and a static method can be accessed without creating objects of that class. As main() is the entry point in program so till then no objects have been created and we have to access the main() method so to directly call main() without creating object, main() is declared as static.
    • void - void is the return type , which tells the compiler that main is not going to return any value.
    • String args[] - Here main should have a String type array as parameter and name of array can be any valid java identifier generally we use args.
  • The next line - System.out.println("Hello World") - causes Hello World to be printed on console screen.
    Note : println() method belongs to PrintStream class.

Compiling and Executing the Program -

In java compilation and execution take place as follows -
java program compilation and execution process

  • Save the file -
    Save this program with the name of class i.e Demo.java(Note - extension should be .java) at desired location (in our case we are saving in C drive in a folder named Programs). It is not always necessary to save the program file with name of class. However if class is public then there is compulsion that file name should be the same as class name.
  • Now open command prompt by writing cmd in search box or run box. First of all we need to go to the location where program is saved (i.e Programs folder in C drive) as shown below-

  • Compilation -
    The name of compiler for java is javac. Compile program by writing the following command -
    javac Demo.java
    Now press enter key. If there is no error then compiler will create a Demo.class file at the same location where your program is stored.
  • Interpretation -
    After compiling we need to interpret Demo.class file. The interpreter of java is java. Write the following command to interpret program -
    java Demo

    Now press the enter key and output will be displayed on the screen as shown below -