Your First Java Program
Walk through Hello World line by line: the class, the main method, System.out.println, semicolons, and braces.
Time to understand every line of the program you saw:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}public class Main {
In Java, all code lives inside a class — a named container. For now, think of the class as the box your program comes in; classes get their real superpowers in Module 7.
Two rules to know today:
1. The class name (Main) must match the file name — this program must be saved as Main.java. Name mismatch = compile error.
2. public means other code is allowed to see this class. Beginner programs mark everything public — the differences matter later.
public static void main(String[] args) {
This line looks intimidating, so let's defuse it: it declares the main method — the exact place where every Java program starts running. When you run your program, the JVM finds main and executes the instructions between its braces, top to bottom.
For now you only need to recognize this line and type it correctly — every word gets its real explanation later in the course:
public— the JVM is allowed to call itstatic— it can run without any setup (Module 7 explains this properly)void— it doesn't hand back a result (Module 5 covers return values)(String[] args)— it can receive text input from the command line
Every Java program you write in this course starts with this exact line. Typing it becomes muscle memory within a week — and yes, exams do ask you to write it from scratch.
Where does every Java program start running?
- AAt the main method: public static void main(String[] args)
- BAt the first line of the file
- CAt the class declaration
- DWherever you put a start() method
System.out.println("Hello, World!");
This is the line that actually does something visible. Reading it right to left:
"Hello, World!"— the text to print. Text in double quotes is called a string.println— short for print line: print the text, then move to the next line (like pressing Enter).System.out— the standard output, i.e. your screen.
Together: "print this text to the screen and end the line." You will type System.out.println thousands of times — every Java programmer's fingers know it by heart.
System.out.println("Hi"); // prints text
System.out.println(42); // prints a number — no quotes needed
System.out.println(2 + 3); // does the math first, prints 5Note the last line: without quotes, 2 + 3 is a calculation. With quotes, "2 + 3" would be literal text. Quotes are the difference between calculate this and display this.
What does this print?
public class Main {
public static void main(String[] args) {
System.out.println(3 + 4);
System.out.println("3 + 4");
}
}- A7 3 + 4
- B3 + 4 3 + 4
- C7 7
- DError
Three rules of Java grammar you must know from day one.
Rule 1: statements end with a semicolon.A statement is one complete instruction — one sentence. The 'period' at the end is the semicolon ;:
System.out.println("Hi"); // one statement
int x = 5; // another statementForget one, and the compiler stops with an error.
Rule 2: braces group code. The{ } after the class and after main mark where each block begins and ends. Every { needs a matching }. Indentation (the spaces at line starts) is purely for human eyes — but we always indent, because readable code matters.
Rule 3: Java is case-sensitive. System and system are different names. Main and main are different names. Typing system.out.println is one of the most common first-week errors — the compiler will say it cannot find symbol system.A beginner typed this. What happens?
public class Main {
public static void main(String[] args) {
system.out.println("Hello");
}
}- APrints Hello
- BPrints hello
- CCompile error — cannot find symbol 'system'
- DThe program crashes while running
Complete the statement that prints a message and moves to a new line:
System.out.___("Done");