Jan 24, 2026
## Chapter 3: Your First Java Code (Hello World)
Okay, let's look at the most famous piece of code in history.
```java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
```
Students usually look at this and say, "Sir, why is it so long? In Python, it's just `print('Hello')`!"
You're right. Java is "verbose" (it talks a lot). But there is a reason for every word.
1. **`public class Main`**: In Java, every piece of code must live inside a "Class". Think of a class as a container or a box.
2. **`public static void main`**: This is the **Starting Point**. When you run your program, the JVM looks for this exact line. If you change even one letter (like `Main` instead of `main`), it won't work.
3. **`System.out.println`**: This is just Java's way of saying "Hey System, send this text to the Output and start a New Line".
### Examples for Chapter 3:
**1. Basic (The "First Step"):**
Change the text inside the quotes.
```java
System.out.println("Java is fun!");
```
*Teacher's comment:* Semicolons `;` are like full stops in English. If you forget them, Java gets confused and stops reading.
**2. Common Mistake (The "File Name" Disaster):**
If your class is named `Main`, your file MUST be named `Main.java`.
*Student mistake:* Naming the file `hello.java` but writing `public class Main`.
*Result:* `Error: class Main is public, should be declared in a file named Main.java`.
**3. Explanation-Focused (The "main" parameters):**
What is `String[] args`? It's a way to give inputs to your program *before* it starts.
If you run `java Main apples oranges`, then `args[0]` will be "apples" and `args[1]` will be "oranges".
*Analogy:* It's like giving a list of ingredients to a chef before he starts cooking.
**4. Real-Life (The "Silent Program"):**
What happens if you remove `System.out.println`?
```java
public class Main {
public static void main(String[] args) {
// Nothing here
}
}
```
The program runs, but you see *nothing*.
*Teacher's comment:* Students often think the program failed. No, it succeeded! It just had nothing to say. In real-world backend code, many programs run silently in the background for months.
**5. Future Hint (The "Static" Mystery):**
Why do we write `static`?
Usually, to use a class, you have to create an "Object" (like building a house from a blueprint). But `static` means "you can use this without creating an object".
*Analogy:* A public park is `static` (anyone can use it anytime). Your own bathroom is NOT `static` (you need to "own/create" the house first). We'll learn this properly in Chapter 11.