The “Hello World” program is one of the simplest and most popular ways to introduce a programming language. In Java, this program demonstrates the structure of a Java application and serves as a basic starting point for beginners.
Java Hello World Program
Code: Java Hello World
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explanation of the Code
// HelloWorld.java
:- This is a comment in Java. Comments are ignored by the compiler and are used to explain code.
public class HelloWorld
:- A class is the blueprint for an object in Java.
public
makes the class accessible from anywhere.HelloWorld
is the name of the class, and it must match the filename (HelloWorld.java
).
public static void main(String[] args)
:- This is the entry point of every Java application. The JVM starts execution from the
main
method. public
: The method is accessible from anywhere.static
: The method belongs to the class rather than an object.void
: The method does not return any value.String[] args
: This parameter stores command-line arguments passed to the program.
- This is the entry point of every Java application. The JVM starts execution from the
System.out.println("Hello, World!");
:- This prints the text
Hello, World!
to the console. System.out
: Represents the standard output stream.println
: Prints the string and moves the cursor to the next line.
- This prints the text
Java Hello World Program
Steps to Run the Program
- Write the Code:
- Open a text editor or an IDE (like Eclipse, IntelliJ IDEA, or VS Code).
- Save the file as
HelloWorld.java
.
- Compile the Program:
- Open a terminal or command prompt.
- Navigate to the folder containing
HelloWorld.java
. - Run the command:
javac HelloWorld.java
- This compiles the Java file into a bytecode file named
HelloWorld.class
.
- Run the Program:
- Execute the program using the JVM with the command:
java HelloWorld
- You should see the output:
Hello, World!
- Execute the program using the JVM with the command:
Common Errors
- Filename Mismatch:
- The class name must match the filename (e.g.,
HelloWorld
→HelloWorld.java
). - If the names do not match, you will get an error like:
Error: Could not find or load main class HelloWorld
- The class name must match the filename (e.g.,
- Missing Semicolon:
- Java statements end with a semicolon (
;
). Forgetting it will cause a compilation error.
- Java statements end with a semicolon (
- Case Sensitivity:
- Java is case-sensitive. Ensure that method and class names use the correct capitalization.
Variations of Hello World
- Using Variables:
public class HelloWorld {
public static void main(String[] args) {
String message = "Hello, World!";
System.out.println(message);
}
}
- Adding User Input:
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Key Takeaways
- Java programs start execution from the
main
method. - A compiled Java program generates a
.class
file, which the JVM executes. - Java’s simplicity and clear syntax make it beginner-friendly.