Your First Java Program

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, not Helloworld.
  • Same capitals. Java cares about case, so H and W stay 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.

Terminal window
javac HelloWorld.java

If 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.

Terminal window
java HelloWorld

And there it is.

Output

Hello, World!

That is the same write, compile, run cycle used to build huge systems. Why two steps?

  • javac checks your code and translates it once into bytecode. You pay this cost a single time.
  • java then runs that bytecode again and again, with no recompile.
  • The bytecode is not tied to one machine, so the same .class file 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.

  • public means other code is allowed to use this class.
  • class is the keyword that says “a class begins here”.
  • HelloWorld is the name you chose, the one the file must match.
  • { opens the block. Everything until the matching } belongs to HelloWorld.
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.

  • public means it can be reached from outside, which the JVM needs.
  • static means the JVM can run it without first creating an object. Just required here for now.
  • void means the method gives nothing back when it finishes.
  • main is the exact name the JVM looks for, all lowercase.
  • String[] args lets 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.

  • System is a built-in class Java gives you for free.
  • out is the standard output, which is your screen.
  • println means “print this, then move to a new line”. The ln stands 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.java
public 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.java
public 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 end
System.out.println("Hello, World!")
// ✅ Good: ends with a semicolon
System.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 point
public static void Main(String[] args) { }
// ✅ Good: lowercase main is what the JVM looks for
public 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.

Terminal window
# ❌ Avoid: adding the extension when you run
java HelloWorld.class
# ❌ Avoid: wrong capitals, so the JVM cannot find the class
java helloworld
# ✅ Good: the class name, exact capitals, no extension
java HelloWorld

Could not find or load main class

Error: Could not find or load main class HelloWorld

This 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 String
System.out.println(“Hello, World!”);
// ✅ Good: plain straight quotes
System.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, like HelloWorld or BankAccount. 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 .class bytecode file.
  • ✅ You run with java HelloWorld, using the class name with no extension.
  • ✅ The program starts from the main method, 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. 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. 2

    Which command compiles a Java file into bytecode?

    Why: javac is the compiler. It turns HelloWorld.java into HelloWorld.class bytecode.

  3. 3

    Where does a Java program start running?

    Why: The JVM looks for the main method and begins the program there.

  4. 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.

Java Program Structure

Share & Connect