A program is a list of instructions a computer follows, one after another, top to bottom. Computers are fast but take everything literally: they do exactly what the instructions say, even when that isn't what you meant. Programming is learning to say what you mean precisely.
Instructions are written in a programming language, a kind of text that both people and a computer can read. This course uses Java, one of the most used languages in the world: it runs banks' back offices, Android apps, big websites, Minecraft and a lot of the software companies build for themselves. It's a good first language because it's strict: it tells you about many mistakes before your program even runs.
Here is a complete Java program. It shows one line of text on the screen:
Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Prints
Hello, world!
That's a lot of words for one line of output, so here's what each part does. You don't need to remember it all yet; every program in this course starts the same way, and later lessons explain the words properly.
public class Main { ... }: Java keeps all code inside a class, a named container. This one is called Main. The curly brackets { and } mark where it starts and ends.
public static void main(String[] args) { ... }: a method called main. When you run the program, Java starts at the first line inside main and works down.
System.out.println("Hello, world!");prints (shows) the text in the brackets and moves to a new line. Text goes in double quotes so Java knows it's text, not an instruction.
The ; ends the instruction, like a full stop ends a sentence. Leaving it out is the most common first mistake.
Running it
Java can't run your text directly. First a program called the compiler (javac) checks the whole file and turns it into bytecode, a compact form the computer can run quickly. Then java runs that bytecode. If the compiler finds a mistake, it stops and tells you the line, and nothing runs.
Terminal
$ javac Main.java # compile: checks the file and makes Main.class
$ java Main # run it
Hello, world!
To try the examples you can install a free JDK (Java Development Kit, for example Eclipse Temurin, version 21 or later) and type the commands above, or paste the code into any online Java editor. A file with public class Main must be called Main.java, capital M included.
More lines, and comments
A program can print as many lines as you like, in order. System.out.print (without ln) prints without moving to a new line, so the next print carries on where it left off. Anything after // on a line is a comment: a note for people, which Java skips.
Java
public class Main {
public static void main(String[] args) {
// say hello in three parts
System.out.print("Good ");
System.out.print("morning");
System.out.println("!");
System.out.println("Numbers don't need quotes:");
System.out.println(6 + 1);
System.out.println("6 + 1");
}
}
Prints
Good morning!
Numbers don't need quotes:
7
6 + 1
See the last two lines: 6 + 1 without quotes is a sum, so Java works it out and prints 7. In quotes it's just text, printed exactly as written.
From the next lesson, most examples show only the lines that go inside main, to keep them short. To run one, put it between the main brackets of the program above.
Practice
What does this print?
System.out.println("2 + 3");
More info
Quick reference
program
Instructions the computer follows in order, top to bottom.
public class Main { }
The container every Java program's code lives in; the file is Main.java.