Java First Hello World Program

In this chapter, we will discuss how to create, compile and run a Java hello world program.
In this chapter, we write our first Java application. The instructions to create a Java program is applicable to users of Windows Vista, Windows 7, and Windows 8. Instructions.

1. Prerequisites

To write your first program, you'll need:
  • The Java SE Development Kit 8 (JDK 8)
You can download the Windows version now. (Make sure you download the JDK, not the JRE.) Consult the installation instructions.
  • A text editor
In this example, we'll use Notepad, a simple editor included with the Windows platforms. You can easily adapt these instructions if you use a different text editor.
These two items are all you'll need to write your first application.

2. Creating Your First Application

Your first application, HelloWorldApp, will simply display the greeting "Hello world!". Let's follow below three steps to create, compile and run this HelloWorldApp program:
  1. Create a source file
A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
  1. Compile the source file into a .class file
The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
  1. Run the program
The Java application launcher tool (java) uses the Java virtual machine to run your application.

1. Create a Source File

Let's first, start the editor. You can launch the Notepad editor from the Start menu by selecting Programs > Accessories > Notepad. In a new document, type in the following code:
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
Before saving file, let's create a directory named myapplication C drive. Now save the code in a file with the name To do this in Notepad, first, choose the File > Save As menu item. Then, in the Save As dialog box: 
By looking into the above source code, let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
  • class keyword is used to declare a class in java.
  • public keyword is an access modifier which represents visibility. It means it is visible to all.
  • static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create an object to invoke the main method. So it saves memory.
  • void is the return type of the method. It means it doesn't return any value.
  • main represents the starting point of the program.
  • String[] args is used for command line argument. We will learn it later.
  • System.out.println() is used print statement.

2. Compile the Source File into a .class File

To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is myapplication on the C drive, type the following command at the prompt and press Enter:
cd C:\myapplication
Now the prompt should change to C:\myapplication>.
Now you are ready to compile. At the prompt, type the following command and press Enter.
Now that you have a HelloWorldApp.class file, you can run your program.

3. Run the Program

In the same directory, enter the following command at the prompt:
java HelloWorldApp
You should see the following on your screen:
C:\myapplication>java HelloWorldApp
Hello World!
Congratulations! Your program works!

Key points


Do not use the extension .class in the command line when executing the program. Use java ClassName to run the program. If you use java ClassName.class in the command line, the system will attempt to fetch ClassName.class.class


If you execute a class file that does not exist, a NoClassDefFoundError will occur. If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main), a NoSuchMethodError will occur. 


When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called the bytecode verifier to check the validity of the bytecode and to ensure that the bytecode does not violate Java’s security restrictions. Java enforces strict security to make sure that Java class files are not tampered with and do not harm your computer.


  • The Java source file name must match the public class name in the program. Java source code files must end with the .java extension.
  • Every class is compiled into a separate bytecode file that has the same name as the class and ends with the .class extension.
  • To compile a Java source-code file from the command line, use the javac command.
  • To run a Java class from the command line, use the java command.
  • Every Java program is a set of class definitions. The keyword class introduces a class definition. The contents of the class are included in a block.
  • A block begins with an opening brace ({) and ends with a closing brace (}).
  • Methods are contained in a class. To run a Java program, the program must have a main method. The main method is the entry point where the program starts when it is executed.
  • Every statement in Java ends with a semicolon (;), known as the statement terminator.
  • Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program.
  • In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. Comments are ignored by the compiler.
  • Java source programs are case sensitive.
  • Java bytecode can be executed on any computer with a Java Virtual Machine.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course