Your first program, line by line
Write Hello World in Java, understand every word in it, and learn the mistakes beginners make most often.
Every Java journey starts with one small program that prints a line of text. It's short, but every word in it means something. Let's write it, run it, and then read it slowly, line by line.
01Write it and run it
Create a file called Hello.java and type this:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}Hello, Java!
Run it from the terminal:
javac Hello.java
java Hello02Line 1: the class
public class Hello {Every piece of Java code lives inside a class. Think of a class as a container for your code. public means any other code may use it, and Hello is its name.
03Line 2: the main method
public static void main(String[] args) {This is where your program starts. When you type java Hello, the JVM looks for exactly this method and runs it. Word by word:
| Word | Meaning |
|---|---|
public | The JVM can call it from outside the class |
static | It belongs to the class, so the JVM can call it without creating an object first |
void | It doesn't return any value |
main | The special name the JVM looks for |
String[] args | Words typed after the program name on the command line arrive here |
04Line 3: printing
System.out.println("Hello, Java!");Systemis a built-in class.outis the standard output, usually your terminal.printlnprints the text and then moves to a new line.
Every statement in Java ends with a semicolon ;.
05Using args
Let's make the program greet whoever you name on the command line:
public class Greet {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "friend";
System.out.println("Hello, " + name + "!");
}
}java Greet.java VivekHello, Vivek!
The live editor (▶ Run) doesn't pass command-line arguments, so there it prints Hello, friend!. That's the fallback in action.
06Common beginner mistakes
- Wrong file name: the public class name and the file name must match exactly.
- Missing semicolon: every statement ends with
;. - Wrong capitals: Java is case-sensitive.
system.out.printlnandMaininstead ofmainboth fail. - Running the .class file name: type
java Hello, notjava Hello.class.
07In the interview
Common follow-up questions:
- Can you overload main? Yes, you can write other methods called
mainwith different parameters, but the JVM only starts the program frommain(String[] args). - What happens without static? The class compiles, but on older Java versions the JVM can't start it. Newer versions add simpler forms of
main, which we cover in the Java versions module. - What is args used for? It holds the words passed on the command line after the program name.
08Check your understanding
Quiz · 4 questions
0 of 4 answered
1. Your file is called hello.java and contains public class Hello. What happens when you compile it?
2. Why is main declared static?
3. What does System.out.println do?
4. You run java Greet.java Asha. What is args[0]?