Lesson · Java Roadmap & Environment
Your first program, dissected
12 min readFirst Program
Hello World is not a ceremony — it is the whole toolchain in miniature. Write, compile, run, and decode what every keyword in that main signature is doing.
Write it, compile it, run it
Create Hello.java, compile it with javac, run the class with java. That is the entire workflow the IDE has been hiding from you — and seeing it once makes every later build tool legible.
javac is a program like any other: it reads your source, type-checks it, and *writes a new file* rather than running anything. If the source has errors, nothing runs — compile time is all-or-nothing. java then takes the class name, starts a JVM, and asks it to run the compiled result. Two commands, two very different jobs.
The classic failures are worth recognising on sight: javac: file not found (wrong directory), class Hello is public, should be declared in a file named Hello.java (name mismatch — the compiler is strict about this for public classes), and Error: Could not find or load main class Hello (you ran java from the wrong directory, or a package path was omitted).
Decoding public static void main(String[] args)
Every keyword was chosen so the JVM can call your method without creating anything or being told its name. Read it as five answers:
- public — the launcher lives outside your class, so the method must be reachable from outside.
- static — there is no object of your class yet; the JVM needs a method it can call on the class itself.
- void — no caller is waiting for a return value; the process ends, and the exit code carries the outcome.
- main — a fixed name. The launcher searches for exactly this name with the right signature.
- String[] args — the command-line arguments, split on spaces. It is never null — with no arguments it is an empty array, not a null. (FYI:
String... argsworks too, varargs and arrays share a type.)
The real output is bytecode
After javac, look in the directory: alongside Hello.java sits Hello.class — bytecode. It is not a runnable app and not machine code; it is instructions for the JVM, carrying the class's structure and a constant pool of names and values.
That file is the deliverable. Copy it to any machine with a JVM and it runs — the same class works on Windows, Linux, ARM and x86, which is what 'compiled once' actually bought you. The lifecycle chapter follows this file from here; the tools chapter shows you how to read it with javap.
Beyond the classic path
The two-step dance has modern shortcuts you should know about. Single-file source launch (Java 11+): java Hello.java compiles in memory and runs immediately — perfect for scripts and experiments, no .class left behind. Compact source files with an instance main (newer JDKs) shrink the ceremony further for small programs, but the classic public static void main remains what every real codebase uses.
One more habit: exit codes. System.exit(0) means success; any non-zero value tells the shell (and CI) something failed. Your program's first real interaction with the operating system is its exit status — scripts and build pipelines are reading it.
The file name must match the public class name
class Hello in Hello.java — for public classes the compiler enforces it. Rename one without the other and javac refuses with 'should be declared in a file named…'. It looks pedantic; it exists so the class loader can find a public class by name before ever reading the file.
Interview angles
Why is main static?+
Because the JVM calls it to bootstrap the program — before any object of your class exists. A static method can be invoked on the class itself, so no instance needs to be constructed first.
What does javac produce, and what runs it?+
javac emits a .class file of bytecode — instructions for the JVM, not native code. The java launcher starts a JVM, loads that class, and the JVM executes (interpreting, then JIT-compiling hot paths).
How do you run a single Java file without a build step?+
java Hello.java (Java 11+): the launcher compiles the file in memory and runs it. Handy for scripts and one-off experiments; production code still compiles to .class/jars.
What exactly is args — can it be null?+
It's the command-line arguments as a String array, one element per space-separated token. With no arguments it's an empty array, never null — so args.length checks are always safe. String... args is the same signature in varargs clothing.