




- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


Java Hello World Program - Your First Step Into Java
Writing Your First Java Program
Create a file named HelloWorld.java
. Java is strict about naming — your filename must match the class name if it's public. HelloWorld in HelloWorld.java
should be same as HelloWorld in public class HelloWorld
.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compiling the Program
Open a terminal or command prompt where your HelloWorld.java
file is saved and run the following command:
javac HelloWorld.java
What happens during compilation?
The javac
command invokes the Java compiler. Here's what it does behind the scenes:
- It reads your
.java
source file — which contains human-readable Java code. - Then it checks for syntax errors or code issues.
- If everything is correct, it converts the source code into bytecode — a platform-independent intermediate form of the code.
- This bytecode is saved in a new file named
HelloWorld.class
.
This bytecode is what the Java Virtual Machine (JVM) understands and executes — not the original source code.
Running the Program
Now, execute the compiled bytecode using the java
command:
java HelloWorld
What happens during execution?
The java
command calls the JVM (Java Virtual Machine) to:
- Locate the
HelloWorld.class
file. - Load the bytecode into memory.
- Interpret or Just-In-Time (JIT) compile it into native machine code specific to your system.
- Execute the
main
method of the class.
Hello, World!
You’ve just compiled and executed your first Java program. This process — compile once, run anywhere — is what makes Java so powerful and portable.
Common Issues to Watch For
- Filename mismatch: Ensure your filename is
HelloWorld.java
if your class isHelloWorld
. - Missing main method: Java won’t run if it doesn’t find the exact
public static void main
method. - Case sensitivity: Java is case-sensitive.
Main
andmain
are not the same.