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).

Java

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.

  1. Everything lives inside a class

    Java
    • 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.

  2. The main method – the entry point

    Java

    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!”

  3. Printing to the screen

    Java
    • 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:

    Java

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:

  1. You write source code File: HelloWorld.java
  2. Compile it using javac (Java compiler)
    Bash
    • This creates a bytecode file: HelloWorld.class
    • Bytecode is platform-independent (same on Windows, Mac, Linux)
  3. Run it using java (the JVM launcher)
    Bash
    • 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):

Bash

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:

  1. Single-line comment
    Java
  2. Multi-line comment
    Java
  3. Javadoc comment (special — used to generate documentation)
    Java

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)
    Java
  • 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!

Java

(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

  1. Create HelloWorld.java in your IDE or notepad.
  2. Compile and run it in terminal and in IntelliJ.
  3. Change the message to something personal (maybe in Marathi or Hindi!).
  4. Add 2–3 more println lines — print your name, city, favorite color.
  5. 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! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *