First Program in Java

Hello World

In Java, every application begins with a class name, and that class must match the filename. Let's create our first Java file, called, which can be done in any text editor (like Notepad).

The file should contain a "Hello World" message, which is written with the following code:

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

Don't worry if you don't understand the code above - we will discuss it in detail in later chapters. For now, focus on how to run the code above.

  • Save this code as "". Open Command Prompt, locate the directory where you saved your file, and type "javac"

  • This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java HelloWorld" to run the file.

You Will see the output as below:

Hello World!

Congratulations! You have written and executed your first Java program.

Since we have already installed IDE, we will use IDE from now to execute our program. If you haven't yet, Learn How to install, configure and execute java program in IDE by clicking here.

Important Java Terminologies Previous Next Java Syntax