Your First Java Program
A line-by-line dissection of the smallest meaningful Java program, with no part left mysterious
It is time to write Java for real. We will take the tiniest possible useful program and explain every single piece of it. By the end of this page, no Java program should ever look intimidating again, only larger.
The program
Run it. You should see Hello, Java! in the output area. Now, let
us pull this program apart.
Line by line
public class Main {
Every Java program lives inside a class. A class is a kind of "file" — a named container for code. We will spend an entire chapter on classes soon. For now, all you need to know is that there is no such thing as a "free-standing" function in Java. Every method belongs to some class.
public— this class is visible to any other code.class— the keyword that declares a class.Main— the name of the class. By convention class names use PascalCase (capital letter for every word).{— opens the body of the class.
By convention, the file is named exactly
Main.java. Java is picky about this: apublic class Mainmust live in a file calledMain.java. CheerpJ in your browser tolerates this in the single-file code blocks, but it is the rule for real projects.
public static void main(String[] args) {
This is the entry point of the program — the very first method the JVM calls when you run your program. Every Java program needs exactly one method that looks (almost) exactly like this.
Break it down:
public— callable from anywhere (including by the JVM).static— belongs to the class itself, not to an instance. This matters because the JVM needs to callmainbefore it has built any objects. (More on this in the classes chapter.)void— returns nothing. When the method ends, the program ends.main— the special name the JVM looks for.(String[] args)— accepts an array of strings; the command-line arguments. In our browser environment this is empty, but on a real computer runningjava Main hello world,argswould be["hello", "world"].
System.out.println("Hello, Java!");
This is the only line that does anything. It prints text.
System— a class from the Java standard library. It exposes things related to the running program.out— a field ofSystemthat is the standard output (the console).println— a method onoutthat prints its argument followed by a newline."Hello, Java!"— a string literal — a value of typeString.;— the semicolon that ends every statement in Java.
The closing braces
Every { needs a matching }. The first close brace ends main;
the second ends the class. Indentation is just for humans;
Java does not care about whitespace. But you very much should.
A picture of what runs
The JVM started, found Main.main, called it, and main made one
call into System.out.println. Then it ended. That's it.
Try changing things
Real understanding comes from breaking things on purpose. Try each of the following in the code block above, and predict what will happen before you click Run.
- Change
printlntoprint(noln). The newline disappears. - Add a second
printlnline. Both lines print. - Add
System.out.println(5 + 3);— Java prints8. You can print numbers, not just strings. - Delete the semicolon at the end of any statement. Java will refuse to compile and tell you what is wrong.
- Rename
maintostart. The JVM cannot findmainand refuses to run.
This is the most important habit of all: break things and watch the error message. Java's compiler is wordy but rarely cryptic.
What is the role of the main method in a Java program?
It is the only method that may print to the screen
It is the method called automatically by the JVM when the program starts
It is required to be called Main like the class
It is optional — Java programs without a main method still run
Why does main have to be declared static?
Because static methods are faster
Because static means the same as public
Because the JVM needs to call main before any object of the class has been constructed
Because static is required for every method in Java
Multi-file Java: a sneak peek
Every "real" Java program is spread across many files — usually one class per file. The code block component supports this. Here is the same greeting, split across two files:
Do not worry yet about what new, private final, or this mean.
The point is: Main no longer does the work itself. It creates a
Greeter object and asks the greeter to do it. That is the seed
of object-oriented programming, and we will plant it properly in a
few pages.
A small challenge
Write a program that prints exactly these three lines:
Name: Ada Lovelace
Year: 1815
Role: First programmer
You now own the basic structure of every Java program you will ever
write. The next page is about what happens when you write int x = 5; — the heart of how a program holds onto data.