Write Your First Java Program – Easy as Pie!

Ever wondered how the “Hello, World!” line actually works behind the scenes? Knowing the skeleton of a Java program is the first step to becoming a coding wizard.

💡 In Simple Words: A Java program is like a sandwich – you need a slice of bread (the class), a tasty filling (the main method), and a sprinkle of instructions (statements). Put them together in the right order, bake (compile) it, and you get a delicious output.

What is the basic structure of a Java program?

Every Java file follows the same backbone:

  • Class declaration – starts with the word public class followed by the class name (the file name).
  • main method – the entry point where the computer starts running your code. It always looks like public static void main(String[] args).
  • Statements – the actual commands you want the computer to execute, ending with a semicolon.
  • Closing braces – { } that tell Java where a block of code begins and ends.

Why does the main method matter?

The main method is like the front door of a house. No matter how many rooms (other methods) you have, the program can’t start until someone walks through that door.

Step‑by‑step: Writing your first Java program

Let’s walk through the process as if you were building a LEGO set.

  1. Open a text editor (Notepad, VS Code, or any IDE).
  2. Create a new file named HelloWorld.java. The name must match the class name exactly.
  3. Type the skeleton – copy the code block below.
  4. Save the file in a folder you can easily find.
  5. Compile the program by opening a command prompt and typing javac HelloWorld.java. The compiler (a tool that translates Java into bytecode) will create HelloWorld.class if there are no errors.
  6. Run the program with java HelloWorld. You should see Hello, World! printed on the screen.
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

What each line does

LinePurpose
public class HelloWorld {Defines a class named HelloWorld. Think of it as the blueprint for your program.
public static void main(String[] args) {Declares the main method – the starting point. public means visible everywhere, static means it belongs to the class itself, void means it returns nothing.
System.out.println("Hello, World!");Prints the text inside the quotes to the console. System.out is Java’s built‑in “screen”.
}Closes the main method block.
}Closes the class block.

Quick Summary Table

ComponentKey WordsAnalogy
Classpublic class ClassNameBlueprint of a house
Main methodpublic static void main(String[] args)Front door of the house
StatementSystem.out.println(...);Instruction to a robot
Brace{ }Walls that contain rooms

Common Mistakes to Avoid

  • File name and class name don’t match – Java will refuse to compile.
  • Missing semicolon at the end of a statement – the compiler throws an error.
  • Wrong case in keywords (Public instead of public) – Java is case‑sensitive.
  • Forgetting the public static void main line – the program has nowhere to start.
graph TD A[Write code] --> B[Save .java file] B --> C[Compile with javac] C --> D[Run with java] D --> E[See output]

📝 Likely Exam Questions

  1. Write the complete structure of a Java program that prints "Welcome".
    Answer:
    public class Welcome {
        public static void main(String[] args) {
            System.out.println("Welcome");
        }
    }
  2. What is the role of the main method in a Java program?
    Answer: It is the entry point where the JVM (Java Virtual Machine) begins execution; without it the program cannot start.
  3. Explain why the class name and file name must be identical.
    Answer: The Java compiler looks for a public class that matches the file name; a mismatch causes a compilation error.
  4. Identify the error in the following line: System.out.println("Hello World")
    Answer: Missing semicolon at the end of the statement.
  5. List the four essential parts of a simple Java program.
    Answer: Class declaration, main method declaration, statements (e.g., print statements), closing braces.
#ICSE#Class 10#Java#Programming#Computer Applications