A Simple Java Program

In this tutorial, we will start by writing our first Java program. It will cover the very basic structure of a program and also the rules that are followed to write a successful Java program. We will be using a console/terminal to compile our program, run and display the output of our program.

Let’s write our first simple Java program that prints “Hello world !” message in the console. At first, open your favorite text editor. Some of the freely available text editors are Notepad++, Atom, and Visual Studio Code which you can use for this tutorial.

Create a new text file and type the following program.

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

The above program is composed of two components:

  1. The class block
  2. The main function

Save the following program in your preferred directory and name it FirstProgram.java

Once saved, open cmd prompt and go to the directory where you have saved the program. (For this tutorial, I have saved the program in the D: drive)

Type the following line and press Enter: javac FirstProgram.java

Javac First Program

The above line will use the java compiler to compile the Java program. It will fail to compile if there is an error in the program. If there are no errors, then it will generate a FirstProgram.class file in the same directory.

After a successful compilation of the program, Type the following line and press Enter. java FirstProgram

This actually executes the Java program and print the output on the next line.

Hello World Program

Congratulations! you have successfully written and executed your first Java program.


  1. We have named our class as the first program, you can name it anything you want. Make sure you give it a valid name as per the name rules.
  2. By convention, a class name starts with a capital case and subsequent internal words are started with capital letters too. Example: CalculatorProgram, MyJavaProgram, HelloWorldProgram, etc.
  3. We cannot use a special character in the class name such as !,*,^,%, etc. Only underscore (_) is allowed.
  4. The first character of a class name should always be an alphabet or an underscore. The class name should not start with a number or other special characters.
  5. For this tutorial, we have saved the file with the name FirstProgram.java which is exactly the same as the class name which makes it easy to remember for compiling and executing.
    When we compile the program with javac command, it generates compiled .class files for that program. They are not generated if there is an error in the program.
    For example, if you have a file MyProgram.java which has a class with the name Calculator, then you will compile the program as: javac MyProgram.java
    To execute the program, you will type: java Calculator
  6. The words public, static, void, class are keywords that should be in small-casing.
  7. The words String and System are pre-defined classes.