Java Hello World Program (With Main Method and System.out.println)
This tutorial will guide you through writing a basic Hello World program in Java. This is often the first program written when learning any new language, and Java is no exception.
Whether you're searching for:
-
Hello World for Java
-
Java Hello World Program
-
Java Language Hello World
You’re in the right place!
Introduction
The Hello World program is a perfect starting point to understand the structure of a Java program. It includes the main method, class declaration, and the System.out.println()
statement.
Basic Java Hello World Program
// HELLO WORLD PROGRAM IN JAVA USING MAIN METHOD
// Java System.out.println() function
// Java main() method
public class Main {
// It's the main function of the program
public static void main(String[] args) {
// Step-1 Display a label message
System.out.println("JAVA HELLO WORLD PROGRAM");
// Step-2 Print the actual Hello World message
System.out.println("HELLO, WORLD!");
}
}
Output
JAVA HELLO WORLD PROGRAM
HELLO, WORLD!
Using a Function
// JAVA HELLO WORLD PROGRAM USING A SEPARATE FUNCTION
public class Main {
// This function prints the hello world messages
public static void printMessage() {
System.out.println("JAVA HELLO WORLD PROGRAM");
System.out.println("HELLO, WORLD!");
}
public static void main(String[] args) {
// Call the function to print the message
printMessage();
}
}
Using a Loop
// JAVA HELLO WORLD PROGRAM USING FOR LOOP
public class Main {
public static void main(String[] args) {
// Step-1 Label message
System.out.println("JAVA HELLO WORLD PROGRAM");
// Step-2 Repeat Hello World 3 times using loop
for (int i = 0; i < 3; i++) {
System.out.println("HELLO, WORLD!");
}
}
}
Common Mistakes to Avoid
-
Missing the
main()
method signature. -
Incorrect class name (must match the file name in some IDEs).
-
Forgetting semicolons
;
. -
Not using
System.out.println()
properly.
FAQs
Q: Do I need to install anything to run Java code?
A: Yes, you’ll need the JDK (Java Development Kit) and optionally an IDE like IntelliJ or Eclipse.
Q: What does System.out.println()
mean?
A: It's a standard Java statement used to display output in the console.
Q: Can I write Java without a main()
method?
A: Not for standalone applications — the main()
method is the entry point.