Your first Java program
What Java is for, the class and the main method every program starts from, and how to print a line of text on the screen.
Java, and why it is everywhere
Java is a compiled language that runs on a virtual machine. You write a file of plain text, a compiler turns it into instructions, and the same program then works on a laptop, a phone or a server without being rewritten for each of them.
It has been around since 1995 and it still runs a huge amount of the software people use daily: bank systems, Android apps, and the build tools that other projects are written with. Learning it means reading code that is explicit about types and structure, and that habit carries over to every other language you meet.
A class, and the main method
Every Java program is built from classes. A class is a named container for code, and Java expects the file to be named after it: a public class called Main has to live in a file called Main.java. The compiler is handed that file and looks for the class with the matching name.
Running a program means calling one method, main. The long line public static void main(String[] args) is the doorway: public so the runtime may call it, static so it needs no object first, void because it hands nothing back. Inside its braces you write the steps you want the computer to take, in order.
Printing, braces and semicolons
System.out.println(...) prints what you give it and then moves to a new line. System.out.print(...) leaves the cursor where it is, so whatever you print next continues on the same line. That is the whole of output for now.
Text goes inside double quotes. Curly braces mark where a block starts and ends, a semicolon ends a statement, and two slashes make the rest of the line a comment for the reader. Forgetting a semicolon or a closing brace is the first error most people meet.
What pressing Run actually does
The file you write is source code. A compiler called javac reads Main.java and produces a class file of bytecode — instructions for the Java Virtual Machine rather than for your particular processor. The virtual machine then runs that bytecode.
That extra step is why Java travels. The class file is the same everywhere; each machine needs only its own virtual machine. It is also why a compile error arrives before any output: the compiler checks the whole file first, and a program that fails that check never starts.
You do not see the class file here. The editor sends the source, and the runner compiles and starts it in one go, but the order never changes: compile, then run.
The kit, the runtime and the machine
Three names sit behind the Run button, and keeping them apart explains most of what Java asks of you. The language is the syntax in the file. The JDK is the kit that holds the compiler, the standard library and the virtual machine. The virtual machine is the program that reads a class file and carries out its instructions.
A machine with only the runtime can run a class file but cannot make one, which is why building Java software asks for the larger kit and running it asks for the smaller one. In this course both halves happen on a server: the editor sends the source, the runner compiles it with the kit, and the virtual machine starts the result. Nothing is installed on your computer, and nothing you write depends on which operating system the server happens to use.
Reading the output pane
The output pane shows two different kinds of text. Standard output is what your println calls wrote. A compiler message or a stack trace is the tool talking about your program, not the result the program itself printed.
When a run fails, read from the first line. Java often reports one mistake and then a trail of consequences, and fixing the first frequently clears the rest. If the program compiled and then failed, the trace names the line that stopped it.
The smallest Java program
public class Main {
public static void main(String[] args) {
// Two slashes start a comment: the compiler skips it.
System.out.println("Hello, Java!");
// print leaves the cursor where it is.
System.out.print("no line break yet ");
System.out.println("and now the line ends");
}
}Run it, then make it yours
Press Run and read the four lines of output. Then write a line of your own, and print a number by putting text and the number on either side of a plus sign.
Three mistakes account for most first runs: a missing semicolon at the end of a statement, a brace that opens but never closes, and a name written with the wrong capital letter. Java is case-sensitive all the way down, so system.out.println is not System.out.println, and main is not Main.
Try these next
- Print a three-line ticket with
println, then rewrite it with oneprintand twoprintlncalls. - Delete one semicolon, run, and read the first compiler message; put it back and confirm the program runs.
- Change the greeting and the number, predict the exact output, and then run it to check.
A badge built from print calls
Press Run and check how the shorter print calls join into complete lines. Then change the name and the year, and add a fourth line for a role.
Java cares about capital letters. System, String and Main must be written exactly like that; system or a lowercase main is an error rather than a style choice.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Which method does the runtime call to start a program?
