Your First Java Program: Hello World
The Hello World program is the most basic program in Java and is often used as the first step for beginners. It demonstrates the fundamental syntax of the Java programming language and how to print output to the console.
Code Example:
Hello World Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // Print greeting
}
}
Expected Output:
Hello, World!
Explanation of the Program:
- Class Declaration: The program defines a class named
HelloWorld
. In Java, every program must have at least one class. - Main Method: The
main
method is the entry point of any Java application. It is where the program starts executing. - Print Statement:
System.out.println()
is used to print text to the console. In this case, it prints Hello, World!.
Compiling and Running the Program:
To compile and run this program, follow these steps:
- Open a text editor and copy the code into a new file.
- Save the file as
HelloWorld.java
. - Open the command line or terminal.
- Navigate to the directory where the file is saved.
- Compile the program using the command:
javac HelloWorld.java
. - Run the compiled program using the command:
java HelloWorld
.
Once executed, the program will display the message Hello, World! in the console.