The “Hello World!” program in Java or any other programming languages is the first and basic program that beginners write it. The main purpose of writing this program is to understand the basic syntax and structure of the program.
Writing the hello world program in Java involves understanding concepts such as classes, methods, and the main entry point of a Java application. So, let’s write a Java program in which we will display hello world on the console.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Hello World Program Line-by-Line Explanation
Now, let’s break down the above Java hello world program line by line.
1. public class HelloWorld
- public: This is an access modifier that allows the class to be accessible from other classes.
- class: This is a keyword used to define a class in Java. A class is a blueprint or template for creating objects.
- HelloWorld: This is the name of the class. By convention, the name of a class in Java should start with an uppercase letter and follow CamelCase naming convention.
2. public static void main(String[] args)
This is the main method, which is the entry point for executing any Java program. Let’s break it down further:
- public: This indicates that the main method is accessible from anywhere.
- static: This is a keyword indicating that the main method belongs to the class rather than any instance of the class. It allows the Java runtime to call this method without creating an object of the class.
- void: This specifies that the main method does not return any value.
- main: This represents the name of the method. It is predefined in Java and acts as the starting point of program execution.
- String[] args: This represents an array of String objects. It is used to pass command-line arguments to the program.
3. System.out.println(“Hello World!”);
- System: This is a predefined class in the java.lang package. It provides access to system-level resources.
- out: This is a static member of the System class and represents the standard output stream.
- println: This is a method of the PrintStream class (to which System.out belongs) in Java. It prints the specified string on the console and then moves the cursor to a new line.
- “Hello World!”: This is the string literal that will be printed on the console.
How to Run Program in Java?
To execute the above hello world program in Java, you should follow all the below steps:
- Write the Code: Use a text editor or an Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Visual Studio Code to write the Java program.
- Save the File: Save the file with the name HelloWorld.java. The file name must match the class name.
- Compile the Code: Open a terminal or command prompt, navigate to the directory containing the file, and run the following command:
javac HelloWorld.java
This command compiles the Java file and generates a bytecode file named HelloWorld.class, which is then executed by the Java Virtual Machine (JVM).
- Run the Program: Execute the program by running the following command:
java HelloWorld
You should see the following output on the console:
Output:
Hello World!
Common Errors and How to Avoid Them
1. Missing Semicolon: Every statement in Java must end with a semicolon. Forgetting this will result in a compilation error. For example:
System.out.println("Hello World!")
Error:
Error: ';' expected
2. Incorrect Class Name: The file name must match the class name. If the file is named hello.java but the class is HelloWorld, you will get an error.
Error: Could not find or load main class HelloWorld
3. Case Sensitivity: Java is a case-sensitive programming language. For instance, System must be written with an uppercase “S”. If you write it as system will cause an error.
Error: cannot find symbol
4. Missing main Method: If you do not define the main method correctly, the JVM will not know where to start the execution of program.
Error: Main method not found in class HelloWorld
These are four common mistakes that beginners often do while learning Java programming. You should remember these common errors.
Enhancements and Variations
Once you have understood how to write the basic hello world program in Java, you can make variations to enhance your understanding by the following below code.
1. Print Multiple Lines:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Welcome to Java programming!");
}
}
Now, you do this variation in your code and execute it. You will get the following output.
Output:
Hello World!
Welcome to Java programming!
2. Use Variables:
public class HelloWorld {
public static void main(String[] args) {
String message = "Hello World!";
System.out.println(message);
}
}
While executing this code, you will get the following output on the console.
Output:
Hello World!
The “Hello World!” program is the simplest program in Java or any other programming languages. You should write this program and execute it because it helps you to understand the basic structure and elements involved in it. Happy coding!