Java "Hello, World!" Program
The Java "Hello, World!" program is a simple and fundamental program that every beginner starts with when learning Java. It prints the message "Hello, World!" on the screen. This basic program introduces you to the structure of a Java program, including key components like classes, methods, and the main() method, where the execution begins.
Let us now head over to the program to print “Hello, World” on the output screen.
Program
Java
// HelloWorld.java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

You can also try this code with Online Java Compiler
Run Code
Compile
Open the command prompt (or terminal in linux) and navigate to the directory where the file HelloWorld.java is saved. Then run the following command to compile the Java program.
javac HelloWorld.java
Run
java HelloWorld
Output
Hello, World!
Parameters used in First Java Program
-
Comments
Comments are lines in the program that help explain the code but are ignored by the compiler. They begin with // for single-line comments and /* */ for multi-line comments.
-
Keywords
Reserved words like class, static, and void are keywords in Java. They have specific functions, such as defining classes, methods, or access levels, and cannot be used as variable or class names.
-
Class Declaration
Every Java program requires a class. The class keyword is used to define a class, and the class name should match the file name if it’s a public class.
-
Main Method
The main() method is the entry point of every Java application. It is declared as public static void main(String[] args).
-
Static Keyword
The static keyword allows methods and variables to belong to the class instead of instances. In the case of main(), it ensures that the method is invoked without creating an object of the class.
-
Void Keyword
The void keyword indicates that the main() method does not return any value.
-
System.out.println()
This statement is used to print output to the console. System is a predefined class, out is the output stream, and println() is a method to print the text.
These parameters define the basic structure and functionality of the Java program.
Explanation
-
Comments: Any line in Java that begins with // is a comment. Comments are included in the code to help users understand the program's aim and operation. The Java compiler completely disregards it (an application that translates a Java program to Java bytecode).
-
Keywords: Reserved words are another name for Java keywords. Keywords are specific terms that serve as a code's key. As these are Java's predefined words, they can't be used as variable, object, or class names. We saw a bunch of keywords in the above program:
- class: It is used to declare a class in Java. Remember that every Java program has a class definition, and the name of the public class should match the filename in Java.
- static: Static keyword is meant for attaching parameters (variables and methods) of a class to the class itself rather than to its objects. As the main() method is static, there is no need to create an object to call it.
- void: It is the return type of the method. It means it doesn't return any value.
-
Access Modifiers: Access modifiers in java are used to set the accessibility of a data member. There are four accessibility modifiers – public, private, default and protected. We have curated a blog specifically for access modifiers here.
-
System.out.println(): To print a statement in Java, use System.out.println(). System is a class, out is a PrintStream class object, and println() is a PrintStream class method. You can read more about java methods here.
Check out this article - Upcasting and Downcasting in Java
Must Read: Java System Out Println
Steps to Implement Java Program
- Install the JDK (Java Development Kit)
To run Java programs, you first need to install the JDK on your computer. The JDK includes everything needed to write and run Java code.
- Write the Java Program
Open a text editor or an IDE like Eclipse or IntelliJ and type your Java code. For a "Hello, World!" program, the code includes a class, a main() method, and a print statement.
- Save the Program
Save your file with the .java extension. Make sure the file name matches the class name. For example, if your class is named HelloWorld, save the file as HelloWorld.java.
- Open Command Prompt or Terminal
Use the command prompt (Windows) or terminal (macOS/Linux) to navigate to the folder where your Java file is stored.
- Compile the Program
Compile the program using the javac command, which translates your code into bytecode. Example:
javac HelloWorld.java
If there are no errors, this will create a HelloWorld.class file.
To run the compiled program, use the java command. Example:
java HelloWorld
This will execute the program.
- View Output
After running, you will see the output in the console:
Hello, World!
Frequently Asked Questions
How do I run a Java program in Eclipse?
Step 1: Launch Eclipse and select File > New > Java Project from the File menu.
Step 2: Type in the name of the project and click the Finish button.
Step 3: Select the project you created in the Package Explorer (left-hand side of the window).
Step 4: Select New > Class from the submenu when right-clicking on the src folder. Click the Finish button after entering the Class name.
Step 5: Create and save the programme.
Step 6: Now hit Ctrl+F11 or pick Run from the Run menu, or click the Run button.
Which Java IDE is best for beginners?
There are various great text editors available for Java Programming, for example, IntelliJ IDEA. BlueJ. Oracle JDeveloper, Eclipse, etc.
Can Vscode run Java?
You can use VS Code to read, write, run, and debug Java source file(s) without creating a project.
Conclusion
We discussed the following points in the blog:
- A class declaration that matches the filename is required for every valid Java application (class name and file name should be the same).
- The main method must be defined within the class.
- Starting with the main function, the compiler executes the code.
-
We saw the role of Access Modifiers and Keywords in Java.
Recommended Readings:
You can also consider our paid courses such as DSA in Java to give your career an edge over others!