Learn Java
import com.learnjava.gettingstarted.FirstProgram;

Writing and Running Your First Program in Java

@Concept

The classic first Java program prints "Hello, World!", but the interesting part is what's structurally required to get there. A source file must be named exactly after the public class it contains β€” HelloWorld.java for a class named HelloWorld, case included β€” because the compiler enforces that match. Inside, the JVM's launcher looks for one specific method signature, public static void main(String[] args), as the program's starting point.

Getting there takes two commands: javac to compile, then java to run the result. Since Java 11, a newer shortcut collapses both into one β€” java HelloWorld.java compiles and runs a single file directly without leaving a .class file behind β€” which is convenient for a first run, even though real projects still compile explicitly as a separate step.

@HowItWorks
1

Write the source file

Name it exactly HelloWorld.java, matching the public class name inside β€” the Java compiler enforces filename-to-class-name parity, capitalization included.

2

Compile it

javac HelloWorld.java reads the source, checks it for compile-time errors, and emits HelloWorld.class β€” a JVM bytecode file, not a native executable.

3

Run it

java HelloWorld (no .class extension here) starts a JVM, loads that bytecode, and looks specifically for a public static void main(String[] args) method to call.

4

Execution happens top to bottom

The JVM runs the body of main one statement at a time, in order, until the method returns or the program is told to stop.

5

A newer shortcut exists

Since Java 11, java HelloWorld.java alone compiles and runs a single-file program in one step, skipping the separate .class file β€” handy for quick tests, though real projects still compile explicitly through a build tool.

@Example
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!
1public class HelloWorld must match the file name HelloWorld.java exactly, including capitalization.
2The JVM's launcher specifically searches this class for public static void main(String[] args) as its starting point.
3System.out is Java's standard output stream, and println writes the given text followed by a newline.
4There is only one println call, so the program produces exactly one line of output before main returns and the program ends.
@Variants

Traditional: Compile, Then Run

Compiles to a .class file first, then runs that bytecode separately β€” the standard two-step approach used by real projects and build tools.

javac HelloWorld.java
java HelloWorld
Output
Hello, World!

Single-File Source Launch (Java 11+)

Skips the separate .class file entirely, compiling and running in memory in one command β€” handy for quick scripts and learning, not typically used for real projects.

java HelloWorld.java
Output
Hello, World!
@UseCases
Verifying a new development environment or IDE project template actually compiles and runs
The canonical first exercise in nearly every Java course, bootcamp, or textbook
Building a minimal reproducible example when asking for help or filing a bug report
A quick sanity check right after switching JDK versions or IDEs

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself β€” building beats watching.