π Writing Your First Hello World Program in Java
π Introduction
The Hello World program is traditionally the first program written in any programming language. It helps beginners understand the basic structure of a program, how code is written, compiled, and executed.
In Java, this program introduces several important concepts such as:
- Class structure
- The
mainmethod - Keywords
- Compilation and execution process
π§± Structure of a Java Program
π Basic Building Blocks
A Java program is composed of:
- A class
- A main method
- Statements inside the method
Every Java application starts execution from the main method.
π§ͺ Hello World Program in Java
π Program Code
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
π Line-by-Line Explanation (VERY IMPORTANT)
πΉ class HelloWorld
π What is a Class?
A class is a blueprint that contains:
- Data
- Methods
- Instructions for the program
In Java, every program must have at least one class.
π Class Name Rules
- Must start with a letter
- Cannot contain spaces
- Usually written in PascalCase
- File name must match class name
β Correct:
class HelloWorld
β Incorrect:
class hello world
πΉ public static void main(String[] args)
π What is the main Method?
The main method is the entry point of a Java program.
Execution always starts from main.
πΉ Breakdown of main Method Keywords
πΈ public
- Makes the method accessible from anywhere
- JVM must be able to access
main
πΈ static
- Allows JVM to call
mainwithout creating an object - Memory efficient for startup
πΈ void
- Specifies that the method returns nothing
πΈ main
- Special method name recognized by JVM
- Execution begins here
πΈ String[] args
- Command-line arguments
- Used to pass data at runtime
argsis just a variable name
Example:
java HelloWorld Ajay Java
πΉ System.out.println("Hello, World!");
π Purpose
Prints output to the console.
π§ Explanation
Systemβ predefined classoutβ output streamprintlnβ prints text and moves cursor to next line
βοΈ How Java Program Execution Works
π Two-Step Process
Java uses a two-step execution model:
- Compilation
- Execution
π οΈ Step 1: Compiling the Program
π Save the File
File name must match class name:
HelloWorld.java
π Compile Using javac
javac HelloWorld.java
β If no error β compilation successful
β Generates:
HelloWorld.class
π§ What Happens During Compilation?
- Source code is converted to bytecode
- Syntax errors are checked
- Bytecode is platform-independent
βΆοΈ Step 2: Running the Program
π Run Using java
java HelloWorld
β Output:
Hello, World!
β οΈ Important Rule
Do not include .java or .class while running.
β Wrong:
java HelloWorld.java
β Correct:
java HelloWorld
π Flow of Execution (Conceptual)
HelloWorld.java
β (javac)
HelloWorld.class
β (JVM)
Output on Screen
β οΈ Common Mistakes Beginners Make
β Mistake 1: File Name and Class Name Mismatch
class HelloWorld { }
Saved as:
Test.java
β Compilation error
β Mistake 2: Missing main Method
class HelloWorld {
}
β Program compiles but does not run
β Mistake 3: Wrong Case in Keywords
Public static void main(...)
β Java is case-sensitive
β Mistake 4: Missing Semicolon
System.out.println("Hello World")
β Syntax error
β Mistake 5: Using Quotes Incorrectly
System.out.println('Hello World');
β Single quotes are for characters, not strings
π§ Why Java Needs This Structure
π Reasons
- Supports Object-Oriented Programming
- Ensures uniform execution
- Enables platform independence
- Allows JVM to manage execution
π Real-World Importance
Even though it is simple, the Hello World program:
- Introduces Java syntax
- Teaches compilation and execution
- Builds foundation for all future programs
Every complex Java application starts with this structure.
π Summary
- Java programs start with a class
- Execution begins from main method
javaccompiles source codejavaexecutes bytecode- File name must match class name
- Java follows a strict but powerful structure
π Conclusion
The Hello World program is more than just printing textβit introduces the core architecture of Java programming. Understanding each component of this program lays the foundation for learning variables, operators, control statements, object-oriented concepts, and advanced Java topics.

