JavaBasicsbeginner
Updated:

Your First Java Program: Hello World Step by Step

6 min read

Write, compile, and run Hello World, then understand every keyword on the line — class, public static void main, and System.out.println — so nothing feels like magic.

TL;DR – Quick Answer

Your first Java program is a class containing a main method that prints text with System.out.println. Save it in a file whose name matches the public class, compile it with javac to produce a .class file of bytecode, then run it with the java command. The JVM starts at the main method, which is the fixed entry point for every Java application.

On This Page

Every Java developer's journey starts with the same eleven lines of code, and understanding those lines completely is worth more than typing a hundred programs you don't understand. This tutorial writes Hello World, then dissects every keyword so nothing is "just how you do it."

You need a working JDK before you start. If java -version does not print a version in your terminal, stop and follow how to install Java first, then come back.

Write the program

Open a plain text editor and create a file named exactly HelloWorld.java. The capitalization matters — we will see why in a moment.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

That is a complete Java program. Save it, and make sure the file name is HelloWorld.java, not helloworld.java or HelloWorld.java.txt (a common trap when an editor hides extensions).

Compile and run it

Java is a compiled language, so there are two steps: turn source into bytecode, then run the bytecode. Open a terminal in the folder containing your file.

javac HelloWorld.java   # step 1: compile -> creates HelloWorld.class
java HelloWorld         # step 2: run the bytecode

The first command, javac, produces a new file called HelloWorld.class containing bytecode. The second command, java, hands that bytecode to the JVM, which executes it and prints:

Hello, World!

Common mistake: Typing java HelloWorld.class in step two. The java launcher wants a class name, not a file name, so you write java HelloWorld with no extension. Adding .class gives you a "could not find or load main class" error that confuses almost every beginner once.

Notice the workflow: compile once, run as many times as you like. Change the source, and you must re-run javac before the change takes effect. That two-step rhythm is Java's compiled nature in action — the same reason Java code is portable across machines is explained in what is Java.

Understand every line

The magic disappears once you can name what each part does. Let's go line by line.

public class HelloWorld

This declares a class named HelloWorld. In Java, all code lives inside a class — there are no free-floating functions like in some languages. The word public is an access modifier meaning any code anywhere may use this class.

This is why the file name must match: a public class named HelloWorld must live in a file named HelloWorld.java. The compiler enforces it so that any class can be found by its name. Rename the class to Greeting without renaming the file and you get a compile error immediately.

public static void main(String[] args)

This is the entry point — the exact method the JVM calls to start your program. Every word is load-bearing:

  • public — the JVM, which is outside your class, must be able to call it.
  • static — it belongs to the class, not to an object, so the JVM can call it without first creating a HelloWorld instance. You will understand this deeply after reading about the static keyword.
  • void — the method returns nothing to the JVM.
  • main — the fixed name the JVM looks for. Spell it Main or mian and the program compiles but refuses to run.
  • String[] args — an array that receives any arguments you pass on the command line.

Change any part of that signature and the JVM will not find a valid entry point.

System.out.println("Hello, World!")

This prints text and moves to a new line. Reading it right to left: System is a built-in class, out is its standard-output stream, and println is the method that writes a line. The text in quotes is a String literal — the argument you are printing.

Pro tip: println adds a newline after printing; print does not. Use System.out.print when you want the next output on the same line. This small distinction shows up constantly once you start building formatted output.

A second program: use the arguments

Hello World never uses String[] args, which makes that parameter feel pointless. Here is a program that actually reads command-line input, so the pieces connect.

public class Greeter {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Hello, " + args[0] + "!");
        } else {
            System.out.println("Hello! Pass your name as an argument.");
        }
    }
}

Compile and run it with an argument:

javac Greeter.java
java Greeter Anitha    # prints: Hello, Anitha!

Run it again as java Greeter with no argument and you get the fallback message. Now String[] args is not abstract — args[0] is the first word you typed after the class name, and args.length tells you how many you passed. The + operator here joins strings together, which you will use constantly; the details are in the strings tutorial.

Common errors and what they really mean

These four cover almost every "it won't run" message from a beginner:

  • class HelloWorld is public, should be declared in a file named HelloWorld.java — your file name and public class name disagree. Rename the file to match.
  • could not find or load main class HelloWorld — you ran java from the wrong folder, added .class, or the class is inside a package. Run it from the folder holding the .class file.
  • javac: command not found — the JDK is not on your PATH. Revisit the install guide; this means the compiler isn't reachable.
  • error: ';' expected — you forgot a semicolon. Every statement in Java ends with one.

Reading the error text instead of panicking is a real skill. The compiler almost always tells you the file, the line, and the problem in plain language. When you hit an error, read it top to bottom: the first error is usually the real one, and the later ones are often just knock-on effects of it. Fix the top error, recompile, and repeat.

Compile-time vs runtime errors

It helps to know which stage failed. A compile-time error comes from javac — a missing semicolon, a misspelled type, a file-name mismatch — and it stops you before any code runs. A runtime error comes from java while the program executes, like a NullPointerException or dividing by zero. The distinction matters because the fix is different: compile errors are about syntax and structure, runtime errors are about the logic and data your program actually meets while running.

Why this matters beyond Hello World

The structure you just wrote — a class, a main method, statements ending in semicolons — is the skeleton of every Java program you will ever write, from a console tool to a Spring Boot service. The public static void main signature in particular is a favorite fresher interview question precisely because it packs four concepts into one line: access modifiers, the static keyword, return types, and arrays.

A question worth answering now: why compile at all, instead of just running the source? Java compiles your .java file into platform-independent bytecode (.class), and the JVM on any operating system runs that same bytecode. That two-step design is what "write once, run anywhere" means in practice — you compile once and the result runs unchanged on Windows, macOS, or Linux. It is also why you never ship .java files to run; you ship the compiled .class files (usually bundled in a .jar).

Once you can compile and run confidently, the next building block is storing data. Move on to variables in Java and then data types to start writing programs that actually compute something. When you are ready to go from isolated syntax to building real applications with guidance, the Java Full Stack course takes you from this first program all the way to deployed projects, and the full Java learning hub maps every topic in between.

Frequently Asked Questions

Why must the file name match the class name in Java?
Java requires that a file's name exactly match its public class name, including capitalization, so the compiler and JVM can locate a class by its name alone. If the public class is HelloWorld, the file must be HelloWorld.java. A mismatch produces a compile error before your code ever runs.
What does public static void main(String[] args) mean?
It is the fixed signature of the entry point the JVM calls to start your program. public makes it callable from outside the class, static lets the JVM call it without creating an object, void means it returns nothing, and String[] args receives any command-line arguments. Change any part and the JVM will not recognize it as the entry point.
What is the difference between javac and java?
javac is the compiler: it reads your .java source file and produces a .class file containing bytecode. java is the launcher: it starts the JVM and runs the bytecode in a .class file. You compile once with javac and can then run the result many times with java.
Why do I get 'could not find or load main class'?
The most common cause is running java with the wrong name or path, such as including the .class extension or being in the wrong folder. Run java HelloWorld with no extension, from the folder that contains HelloWorld.class. Package declarations also change the name you must pass.
Do I need an IDE to run my first Java program?
No. A plain text editor and the command line are enough, and doing it that way once teaches you what an IDE hides. After you understand compile and run, an IDE like IntelliJ or Eclipse speeds you up, but it is worth compiling manually at least once so the process is not a black box.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Explore the Program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 15 July 2026 LinkedIn
Chat with us