A Simple Java Program

In this tutorial, we will start by writing our A Simple Java Program in a text editor. We will then compile the written program and execute it to print a message. We will also focus on the key points on how to name the files that contain the program and other rules of writing a Java program.

A Simple Java Program

Table of Contents

A Simple Java Program

Let’s write our first simple Java program that prints a “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
  • The class block is kind of a wrapper block that contains all the code of a class.
  • A class basically contains all code that relates to each other. For now, our class is named as FirstProgram.
  • The class block has a main() function that is the starting point of the program. (Ignore other keywords as they will be covered later).
  • The statement System.out.println(“Hello World !”); prints the actual message in the console. Feel free to change the message between double-quotations to anything you want to print in the console.

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

Once saved, open the 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
java First ProgramThe 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 prints its output on the next line.

Java Hello Word

Congratulations! Your first Java program is written is executed successfully.


  1. We have named our class as FirstProgram, you can name it anything you want. Make sure you give it a valid name as per the naming rules.
  2. By convention, a class name starts with a capital case, and subsequent internal words are started with capital letters too. Examples: 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 the 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, and class are keywords that should be in small casing.

7. The words String and System are pre-defined classes.