Java Hello World Program
Java is one of the most popular and widely used programming languages and platforms. In this article, we will learn how to write a simple Java Program. This article will guide you on how to write, compile, and run your first Java program. With the help of Java, we can develop web and mobile applications.
In this article, we will learn:
- How to create your first Java program
- How to compile and run Java code
- Understanding the Hello World program structure
Steps to Implement a Java Program
The implementation of a Java program involves the following steps. They include:
- Creating the program
- Compiling the program
- Running the program
1. Create a Java Program
Java programs can be written in a text editor (Notepad, VS Code) or an IDE (IntelliJ, Eclipse, NetBeans).
// Simple Java Hello World Program
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World");
}
}
Note: Save the file as HelloWorld.java
2. Compile the Java Program
To compile the program, we must run the Java compiler (javac), with the name of the source file on the "command prompt" (Windows) or "Terminal" (Mac/Linux) as follows:
javac HelloWorld.java
Note:
- Before running the command, make sure you navigate to the correct directory where your HelloWorld.java file is saved.
- If everything is OK, the compiler creates a file called HelloWorld.class which contains the byte code of the program.
3. Run the Java Program
We need to use the Java Interpreter to run a program. Execute and compile Java program with below command:
java HelloWorld
Output:
Hello, World
Note: Java syntax is similar to C/C++, which makes it easier for programmers who are already familiar with those languages. Java syntax is simple and easy to understand.
Implementation of Java Hello World
The below-given program is the most simple program of Java printing "Hello World" to the screen. Let us try to understand every bit of code step by step.
// Simple Java program
// FileName: "HelloWorld.java"
public class HelloWorld {
// Your program begins with a call to main().
// Prints "Hello, World" to the terminal window.
public static void main(String[] args)
{
System.out.println("Hello, World");
}
}
Output
Hello, World
Understanding the Java Hello World Code
1. Class Definition
Every Java program must have at least one class. This line uses the keyword class to declare that a new class is being defined.
class HelloWorld {
//
//Statements
}
Note: If the class is public, the filename must match the class name HelloWorld.java
2. HelloWorld
It is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace " { " and the closing curly brace " } ".
3. main Method
In the Java programming language, every application must contain a main method. The main function(method) is the entry point of your Java application, and it's mandatory in a Java program.
Signature of main() Method
public static void main(String[] args)
Explanation of the above syntax:
- public: So that JVM can execute the method from anywhere. If we declare something as public it simply means it is accessible from anywhere.
- static: The main method is to be called without creating an object. The modifiers are public and static can be written in either order.
- void: The main method doesn't return any value.
- main(): Name configured in the JVM. The main method must be inside the class definition. The compiler executes the codes starting always from the main function.
- String[]: The main method accepts a single argument, i.e., an array of elements of type String.
Like in C/C++, the main method is the entry point for your application and will subsequently invoke all the other methods required by your program.
The next line of code is shown here. Notice that it occurs inside the main() method.
System.out.println("Hello, World");
Explanation of the above syntax:
This line is used to print the Hello, World on the console Here is the brief description of the above code:
- System: It is the predefined class which is present in the java.lang package which provides the system resources for input and output.
- out: It is a static field of type PrintStream in the System class used to represent the standard output stream on the console.
- . (dot): It is the member access operator also known as link operator used to access the members of class or objects i.e fields and methods.
- println(): It is the method of PrintStream class which is used to print the message on the new line written inside as string"" (enclosed with double quotes).
Comments
They can either be multiline or single-line comments.
// Simple Java program
// FileName: "HelloWorld.java"
This is a single-line comment. This type of comment must begin with // as in C/C++.
For multiline comments, they must begin from /* and end with */.
/*
This is a
multi-line comment
*/
Important Points:
- The name of the class is HelloWorld, which is same as the name of the file "HelloWorld.java". This is not a coincidence. In Java, all codes must present inside a class, and there is at most one public class which contains the main() method.
- By convention, the name of the main class(a class that contains the main method) should match the name of the file that holds the program.
- Every Java program must have a class definition that matches the filename (class name and file name should be same).
Compiling the Program
After successfully setting up the environment, we can open a terminal in both Windows/Unix and go to the directory where the file - HelloWorld.java is present. Now, to compile the HelloWorld program, execute the compiler - javac, to specify the name of the source file on the command line, as shown:
javac HelloWorld.java
The compiler creates a HelloWorld.class (in the current working directory) that contains the bytecode version of the program. Now, to execute our program, JVM (Java Virtual Machine) needs to be called using Java, specifying the name of the class file on the command line, as shown:
java HelloWorld
This will print "Hello World" to the console or terminal screen.
Note: If you get ClassNotFoundException, ensure the .class file is in the correct directory or check your CLASSPATH.
Output:
1. In Windows,

2. In Linux,
