Chapter 2 :First Java Program
2. First Java Program – The Classic “Hello World”
Every programming language starts with a Hello World program — it’s like the first “Namaste” you say when you meet someone new. In Java, it’s a bit more formal than in Python, but once you understand why, it becomes very natural.
Step 1: The Famous Hello World Program
Create a new file called HelloWorld.java (very important: file name must match the class name + .java).
|
0 1 2 3 4 5 6 7 8 9 10 11 |
// This is our very first Java program public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Webliance! Welcome to Java in 2026! ☕"); } } |
That’s it! Now let’s break it down line by line like a human teacher would — slowly and clearly.
Step 2: Understanding the Structure of a Java Program
Java is very strict about structure — every program must follow these rules.
-
Everything lives inside a class
Java012345678public class HelloWorld {// all your code goes inside here}- public → this class is visible to everyone (we’ll talk access modifiers later)
- class → keyword to declare a class
- HelloWorld → name of the class (must match the file name exactly: HelloWorld.java)
- Curly braces { } → define the body of the class
Analogy: Think of a class like a building. Everything (rooms, furniture) must be inside the building.
-
The main method – the entry point
Java012345678public static void main(String[] args) {// Java starts executing from here!}This is the most important line in beginner Java.
Why is it written exactly like this?
Part Meaning Why mandatory? public Anyone can call it (JVM needs to call it) JVM is outside your program static Belongs to the class, not to any object (we can call it without creating an object) JVM calls it before creating any objects void This method does not return any value It’s just the starting point main Special name — JVM looks for exactly this method name Fixed name — JVM searches for main (String[] args) Parameter: array of Strings (command-line arguments) Lets you pass data when running the program { … } Body of the method — where actual code lives — Common newbie mistake: Forgetting static or writing Main instead of main → program won’t run!
Real-life analogy: The main method is like the front door of your house. The JVM knocks on the front door named main to enter your program. If the door is missing or named differently, JVM says: “Sorry, I can’t find the entrance!”
-
Printing to the screen
Java0123456System.out.println("Hello, Webliance! Welcome to Java in 2026! ☕");- System → a built-in class
- out → an object that represents the console (screen)
- println() → prints the message + adds a new line
- You can also use print() (no new line)
Variations you’ll see a lot:
Java012345678System.out.print("Hello "); // no new lineSystem.out.println("World!"); // new line afterSystem.out.println(); // just a blank line
Step 3: Compilation and Execution Process (How Java Really Works)
Java is compiled and interpreted — that’s why it’s both fast and portable.
Here’s the exact flow:
- You write source code File: HelloWorld.java
- Compile it using javac (Java compiler)
Bash0123456javac HelloWorld.java
- This creates a bytecode file: HelloWorld.class
- Bytecode is platform-independent (same on Windows, Mac, Linux)
- Run it using java (the JVM launcher)
Bash0123456java HelloWorld
- Notice: no .class extension when running
- JVM loads HelloWorld.class, verifies it, JIT-compiles hot parts to native code, and executes the main method
Terminal Example (what you should see):
|
0 1 2 3 4 5 6 7 8 9 |
C:\JavaLearning> javac HelloWorld.java C:\JavaLearning> java HelloWorld Hello, Webliance! Welcome to Java in 2026! ☕ |
If using IntelliJ IDEA (recommended):
- Just click the green Run button (triangle) next to the main method — IntelliJ does javac and java for you automatically!
Step 4: Comments in Java – Explaining Your Code
Comments are super important — they help you (and others) understand the code later.
Three types:
- Single-line comment
Java01234567// This is a single-line commentSystem.out.println("Hello"); // This prints Hello
- Multi-line comment
Java012345678910/*This is a multi-line comment.You can write many lines here.Useful for explaining big blocks of code.*/
- Javadoc comment (special — used to generate documentation)
Java01234567891011/*** This is a Javadoc comment.* It appears in auto-generated documentation.* @author Webliance* @version 1.0*/
Tip: Always comment your code — especially when learning!
Step 5: Basic Syntax Rules (Things Java is Very Strict About)
Java has some rules you must follow:
- Every statement ends with a semicolon ; Wrong: System.out.println(“Hi”) Right: System.out.println(“Hi”);
- Java is case-sensitiveMain ≠ mainSystem ≠ system
- Curly braces { } are mandatory even for one line (good practice)
Java012345678if (true) {System.out.println("Always true");}
- File name must match the public class name exactly (including capital letters)
- One public class per .java file
Bonus: Let’s Make It More Fun – Play with Hello World
Try changing the message and running again!
|
0 1 2 3 4 5 6 7 8 9 10 11 |
public class HelloWorld { public static void main(String[] args) { System.out.println("नमस्ते वेबलायन्स! जावा २०२६ मध्ये स्वागत आहे! 🚀"); System.out.println("Current time in Mumbai: " + java.time.LocalDateTime.now()); } } |
(You’ll see the current date & time printed — Java has built-in date-time support!)
Common Errors Beginners Make (and Fixes)
| Error Message | What went wrong? | Fix |
|---|---|---|
| Error: Main method not found | main misspelled or missing static/public | Check spelling and signature exactly |
| class HelloWorld is public, should be declared in file HelloWorld.java | File name doesn’t match class name | Rename file or class to match |
| ‘;’ expected | Forgot semicolon | Add ; at end of statement |
| cannot find symbol | Typo in class name (e.g. system instead of System) | Java is case-sensitive! |
Homework for You
- Create HelloWorld.java in your IDE or notepad.
- Compile and run it in terminal and in IntelliJ.
- Change the message to something personal (maybe in Marathi or Hindi!).
- Add 2–3 more println lines — print your name, city, favorite color.
- Try removing public static void main and see what error you get (learning by breaking!).
You did amazing! This is your first real Java program — celebrate with a cup of cutting chai ☕
Ready for Chapter 3: Variables and Data Types? Or want me to explain any part again with more examples? Just say the word! 😊
