Ever wondered why a simple line like System.out.println("Hello"); can make your computer talk?
💡 In Simple Words: A Java program is a set of instructions wrapped in a specific format. Think of it as a recipe: you list ingredients (variables), steps (methods), and the kitchen rules (syntax) so the computer knows exactly what to do.
How to write a simple Java program
When you open a text editor and type a few lines, you’re not just scribbling – you’re following a tiny blueprint that Java understands. The blueprint has three main parts:
- Class declaration – tells Java the name of the container for your code.
- main method – the entry point where the program starts running. (A method is a named block of code that does something.)
- Statements – the actual instructions, like printing text or doing math.
Step‑by‑step: From typing to seeing output
Basic skeleton of a Java program
Here’s the smallest program that prints Hello, World! on the screen:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Let’s break it down, line by line.
| Line | What it does |
|---|---|
| 1 | public class HelloWorld { – declares a class named HelloWorld. public means anyone can see it; class is a container for code. |
| 2 | public static void main(String[] args) { – the main method. static lets Java call it without creating an object first. void means it returns nothing. String[] args is a place to receive command‑line words, but you can ignore it for now. |
| 3 | System.out.println("Hello, World!"); – tells the system (your computer) to print the text inside the quotes. System.out is the console output stream, and println means “print line” (adds a new line after). |
| 4 | } – closes the main method. |
| 5 | } – closes the class. |
Why the curly braces?
Curly braces { } are like the walls of a room. Everything inside belongs to that room. In our example, the first pair encloses the whole class, and the second pair encloses the main method.
Compiling and running the program
Writing code is only half the battle. The computer speaks a language called bytecode, not plain English. To translate, you use two commands:
- javac HelloWorld.java – the compiler (
javac) turns your .java file into a .class file. - java HelloWorld – the runtime (
java) reads the .class file and executes themainmethod.
If you see Hello, World! on the terminal, you’ve just built and run your first Java program!
Common mistakes to avoid
- Forgetting the
.javaextension when you save the file. - Mismatching the class name and file name (they must be identical, case‑sensitive).
- Leaving out a semicolon
;at the end of a statement. - Using wrong braces – every opening
{needs a matching closing}.
Quick reference table
| Component | Purpose | Typical Syntax |
|---|---|---|
| Class | Container for related code | public class ClassName { … } |
| main method | Program’s entry point | public static void main(String[] args) { … } |
| Statement | Action the program performs | System.out.println("text"); |
| Comment | Notes for humans, ignored by Java | // single line comment |
📝 Likely Exam Questions
- Write the complete structure of a Java program that prints your name.
Answer: Provide a class namedNamePrintwith amainmethod that containsSystem.out.println("Your Name");. - What is the role of the
mainmethod in a Java program?
Answer: It is the starting point where the JVM (Java Virtual Machine) begins execution. Without it, the program won’t run. - Explain why the class name and file name must match.
Answer: The compiler looks for a public class that has the same name as the file. If they differ, compilation fails. - Identify the error in the following snippet and correct it:
Answer:public class Test { public static void main(String args) { System.out.println("Hi") } }String argsshould beString[] args, and a semicolon is missing afterprintln("Hi"). Corrected line:System.out.println("Hi"); - List the four steps to run a Java program from source code.
Answer: Write the code, save with.javaextension, compile withjavac, run withjava.