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 classfollowed 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.
- Open a text editor (Notepad, VS Code, or any IDE).
- Create a new file named
HelloWorld.java. The name must match the class name exactly. - Type the skeleton – copy the code block below.
- Save the file in a folder you can easily find.
- Compile the program by opening a command prompt and typing
javac HelloWorld.java. The compiler (a tool that translates Java into bytecode) will createHelloWorld.classif there are no errors. - Run the program with
java HelloWorld. You should seeHello, World!printed on the screen.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
What each line does
| Line | Purpose |
|---|---|
| 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
| Component | Key Words | Analogy |
|---|---|---|
| Class | public class ClassName | Blueprint of a house |
| Main method | public static void main(String[] args) | Front door of the house |
| Statement | System.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 (
Publicinstead ofpublic) – Java is case‑sensitive. - Forgetting the
public static void mainline – 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
- 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"); } } - What is the role of the
mainmethod in a Java program?
Answer: It is the entry point where the JVM (Java Virtual Machine) begins execution; without it the program cannot start. - 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. - Identify the error in the following line:
System.out.println("Hello World")
Answer: Missing semicolon at the end of the statement. - 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
More from us