Chapter 310 min read
Your First Java Program: Hello World
Understanding the structure, compilation pipeline, and execution of a Java application.
Released1995
Core MottoWrite Once, Run Anywhere
CreatorsOracle / Sun Microsystems
1. Overview
Printing 'Hello World' is the classic tradition for introducing any programming language.
This tutorial covers writing, saving, compiling, and running your very first Java program.
The Classic Tradition
Universal Benchmark:Testing your toolchain and syntax environment.
Immediate Verification:Confirming that the Java Development Kit (JDK) is correctly configured on your machine.
2. Java Program to Print 'Hello World'
Below is the standard source code required to print 'Hello World' to the console output.
Source Code Structure
Complete Class Definition:Encapsulated inside a public class whose name matches the file name.
Execution Entry Point:Contains the standard public static void main method.
java
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}Classroom Discussion Starter
Why must the public class name match the exact file name in Java?
Takeaway Goal: The Java compiler relies on this strict naming convention to locate and verify the public class before reading or compiling the file content.
3. Steps to Write, Save, and Run
Follow the standard command-line workflow to compile and execute your source code.
Execution Workflow
1.
Open a text editor (like Notepad) and add the source code.
2.
Save the file with the exact name:MyFirstJavaProgram.java.
3.
Open a command prompt window and navigate to your directory (e.g., C:\).
4.
Type 'javac MyFirstJavaProgram.java' to compile your code into bytecode.
5.
Type 'java MyFirstJavaProgram' to execute the program via the JVM.
bash
# Compile the source file into bytecode
C:\> javac MyFirstJavaProgram.java
# Run the compiled program using the JVM
C:\> java MyFirstJavaProgram
# Output:
Hello World4. Explanation of Hello World Program
Let's dissect the code line by line to understand how Java structures its components.
1. Public Main Class
public class MyFirstJavaProgram:Declares a public class. The filename must match this class name exactly.
java
public class MyFirstJavaProgram {2. Comment Section
Multi-line Comments:Text enclosed within /
...
/ is ignored by the Java compiler and used entirely for code readability and documentation.
java
/* This is my first java program.
* This will print 'Hello World' as the output
*/3. Public Static Void Main
Entry Point:The main method is called automatically by the Java Virtual Machine (JVM) when loading the program into memory.
java
public static void main(String []args) {4. Keywords Breakdown
public:Defines the visibility; allows external programs like the JVM to call the method.
static:Allows the JVM to invoke the method without creating an object instance of the class first.
void:Indicates that the method does not return any value back to the caller.
main:The mandatory identifier name searched for by the JVM execution engine.
String []args:Accepts command-line arguments passed during runtime execution.
5. System.out.println() Method
Console Output:System.out targets the standard system console, and println prints the string argument followed by a new line.
java
System.out.println("Hello World"); // prints Hello World