Your First Java Program
Table of Contents + −
In the last lesson you learned the difference between JDK, JRE, and JVM. Now you write your first Java program. You’ll compile it, run it, read every word, and break it on purpose so you know what each error looks like.
✍️ Write the code
Make a new file called HelloWorld.java, open it in any editor, and type this in.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); }}That is the whole program. It prints one line and stops. Type it yourself instead of copying, so the shape sinks in.
A word on that file name: a public class must live in a file with the same name.
- Same spelling.
HelloWorld, notHelloworld. - Same capitals. Java cares about case, so
HandWstay capital. - Extension must be
.java. Not.txt, not.java.txt.
The file name must match the class
If the class name and file name disagree, Java refuses to compile. You see class HelloWorld is public, should be declared in a file named HelloWorld.java. Just rename the file to match the class.
⚙️ Compile and run it
Java takes two steps: first you compile (turn your code into a form the machine can run), then you run. Open your terminal in the folder where you saved the file.
Step one compiles the file into bytecode.
javac HelloWorld.javaIf there are no mistakes, this prints nothing and quietly creates HelloWorld.class. Silence means it worked. That .class file holds the bytecode the JVM understands.
Step two runs it. Use just the class name, no .java and no .class.
java HelloWorldAnd there it is.
Output
Hello, World!That is the same write, compile, run cycle used to build huge systems. Why two steps?
javacchecks your code and translates it once into bytecode. You pay this cost a single time.javathen runs that bytecode again and again, with no recompile.- The bytecode is not tied to one machine, so the same
.classfile runs on Windows, Mac, or Linux. That is “write once, run anywhere”.
One step on newer Java
On Java 11 and above you can run java HelloWorld.java to compile and run in one go. The two-step way is still worth knowing, because real projects with many files always compile first, then run.
🔍 What does each line mean?
You typed it. Now read it word by word.
public class HelloWorld {This starts a class named HelloWorld. A class is a container for your code, and in Java all code lives inside a class.
publicmeans other code is allowed to use this class.classis the keyword that says “a class begins here”.HelloWorldis the name you chose, the one the file must match.{opens the block. Everything until the matching}belongs toHelloWorld.
public static void main(String[] args) {This is the main method. When you run a program, the JVM looks for main and starts there. It is the front door.
publicmeans it can be reached from outside, which the JVM needs.staticmeans the JVM can run it without first creating an object. Just required here for now.voidmeans the method gives nothing back when it finishes.mainis the exact name the JVM looks for, all lowercase.String[] argslets the program take input from the command line. Ignore it for now.
System.out.println("Hello, World!");This line does the visible work. Read it left to right.
Systemis a built-in class Java gives you for free.outis the standard output, which is your screen.printlnmeans “print this, then move to a new line”. Thelnstands for line."Hello, World!"is the text to print. Text in double quotes is a String.;marks the end of the statement, like a full stop.
}}The first } closes main. The second } closes the class. Every { needs a matching }, so count them: the two counts must be equal.
🧠 Why so much code for one line?
Some languages print with one short line. Java needs a class and a method first. Here is why.
- Java is built for big programs. The setup feels heavy for one line, but on a large project it keeps thousands of lines organised.
- Everything has a home. Code cannot float around loose. It must sit inside a class, which keeps big code bases tidy.
- It becomes automatic fast. After a few programs you type this shape without thinking, and your editor can fill it in with a shortcut.
⚠️ Common Mistakes
These are the errors you will hit first. Every Java developer has met all of them.
File name does not match the class. A mismatch stops the compile before it even starts.
// ❌ Avoid: class Hello inside a file named HelloWorld.javapublic class Hello { // class name and file name disagree public static void main(String[] args) { System.out.println("Hi"); }}
// ✅ Good: class name matches the file name HelloWorld.javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hi"); }}Fix: make the two names identical, capitals and all.
Forgetting the semicolon. Every statement ends with ;.
// ❌ Avoid: no semicolon at the endSystem.out.println("Hello, World!")
// ✅ Good: ends with a semicolonSystem.out.println("Hello, World!");The compiler shows ';' expected. Fix: add the semicolon.
Spelling main wrong, or wrong capitals. Java is case-sensitive, so Main and main are different words. The same care goes for System and println.
// ❌ Avoid: capital M, so the JVM never finds the entry pointpublic static void Main(String[] args) { }
// ✅ Good: lowercase main is what the JVM looks forpublic static void main(String[] args) { }This code still compiles, but running it fails with the message below.
Could not find or load main class
Error: Main method not found in class HelloWorld, please define the main method as: public static void main(String[] args)The JVM found your class but not a correct main. Check the spelling, the capitals, and that the line reads exactly public static void main(String[] args).
Running the wrong name. You compile fine, then type the run command wrong.
# ❌ Avoid: adding the extension when you runjava HelloWorld.class
# ❌ Avoid: wrong capitals, so the JVM cannot find the classjava helloworld
# ✅ Good: the class name, exact capitals, no extensionjava HelloWorldCould not find or load main class
Error: Could not find or load main class HelloWorldThis almost always means one of three things. You are in the wrong folder, the capitals do not match, or you added a .class or .java extension. Run javac first, then java HelloWorld from the same folder.
Curly quotes instead of straight quotes. Some editors turn " into curly quotes. Java only accepts straight double quotes.
// ❌ Avoid: smart quotes break the StringSystem.out.println(“Hello, World!”);
// ✅ Good: plain straight quotesSystem.out.println("Hello, World!");If you see an odd illegal character error on a line that looks fine, suspect the quotes first.
✅ Best Practices
A few small habits make this cycle smoother from day one.
- Keep one public class per file and name the file after it.
- Compile after every small change. Small steps mean small errors that are easy to fix.
- Read the error from the top. The first error is usually the real one; later errors are often knock-on effects.
- Let your editor format the code, so missing braces and stray semicolons jump out at you.
- Use clear class names in
PascalCase, likeHelloWorldorBankAccount. Each word starts with a capital, no spaces.
🧩 What You’ve Learned
You ran your first program, and you know how to fix it when it breaks.
- ✅ A Java program lives inside a class, and the file name must match the public class name.
- ✅ You compile with
javac HelloWorld.java, which creates a.classbytecode file. - ✅ You run with
java HelloWorld, using the class name with no extension. - ✅ The program starts from the
mainmethod, the front door the JVM looks for. - ✅
System.out.println(...)prints a line, and every statement ends with a semicolon. - ✅ The common errors come from name mismatches, missing semicolons, and wrong capitals.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
If your class is named HelloWorld, what must the file be named?
Why: The file name must match the public class name exactly, so it must be HelloWorld.java.
- 2
Which command compiles a Java file into bytecode?
Why: javac is the compiler. It turns HelloWorld.java into HelloWorld.class bytecode.
- 3
Where does a Java program start running?
Why: The JVM looks for the main method and begins the program there.
- 4
What ends each statement in Java?
Why: Java requires a semicolon at the end of each statement, like after a println call.
🚀 What’s Next?
You wrote your first program, but we breezed past its overall shape. Next, let’s break down the structure of a Java program properly, so every part makes sense before we move into real coding.