Lesson 1Phase 1: First steps
Your first Java program
Install Java, write and run a program from an empty file, and see what the JDK, the compiler and the JVM each do.
By BasithUpdated 50 min
- JDK
- JRE
- JVM
- bytecode
- compile vs run
- main method
Every program starts out as plain text that someone typed into a file. On this page you install Java, write a first program and run it. Along the way you meet the two tools that turn your text into a working program: the compiler, which translates what you wrote, and the Java Virtual Machine, which carries it out.
- Describe the journey from a
.javafile to a running program, and say whatjavac, bytecode and the JVM each do. - Tell the JDK, the JRE and the JVM apart, install a JDK 25, and check it with
java -version. - Write a first program as a compact source file, and the same program in the classic
public class Mainform. - Run a program from a terminal with
javacand thenjava, or in one step withjava Main.java. - Try single lines of Java in JShell.
- Leave comments in your code, and spot the small slips behind most first errors.
From a text file to a running program#
A computer's processor understands only one kind of instruction: its own machine code, long runs of numbers that differ from one kind of chip to another. Nobody writes programs that way. You write source code, text that a person can read, and a tool translates it into instructions the computer can carry out.
Java source code lives in a file whose name ends in .java. Getting from that file to a running program takes two steps.
- 1You compile the file. A tool called
javac, the Java compiler, reads your.javafile, checks it for mistakes, and translates it into a new file whose name ends in.class. What that file holds is called bytecode: a compact list of instructions written for an imaginary computer, which the second step supplies. You cannot read bytecode like text, and your processor cannot run it directly. - 2You run the program. The
javacommand starts the JVM, short for Java Virtual Machine, and hands it the.classfile. The JVM carries out the bytecode instructions one after another, and this is when your program actually does something, such as printing a line on the screen.
So Main.java goes into javac, Main.class comes out, and the JVM runs it.
Why two steps?
The split pays off when a program has to run on different computers. The same .classfile runs on Windows, on macOS and on Linux without being rebuilt. What differs from one system to the next is the JVM: there is a JVM for Windows, one for macOS and one for Linux, and each of them knows how to run the same bytecode on its own system. Java's old slogan for this is write once, run anywhere.
Key ideaYou compile Java once, into bytecode, and that bytecode runs on any computer that has a JVM. The JVM is the part that changes from system to system, so your program does not have to.
.pyfile straight from the source, with no build step you can see. C compiles to one processor's machine code, so a program built for Windows has to be compiled again for a Mac. Java does some of each: it compiles once, to bytecode, and a JVM on each system runs that bytecode.JDK, JRE and JVM#
Three abbreviations come up the moment you try to install Java. They describe layers that fit inside each other, from the smallest outward.
- The JVM (Java Virtual Machine) is the engine that reads bytecode and carries it out. On its own it is an engine with no car around it.
- The JRE (Java Runtime Environment) is the JVM plus Java's standard library, a large collection of ready-made code that almost every program uses, such as the code that prints text. A JRE can run a program that someone has already compiled. It has no compiler, so it cannot build new programs.
- The JDK (Java Development Kit) is the JRE plus the tools for writing programs, most importantly the compiler
javac. This is the one you install.
JVMJREJDKjavac and others). Enough to write, compile and run. Install this one.Key ideaThe JDK contains the JRE, and the JRE contains the JVM. To learn Java you install the JDK, because it is the only one of the three that can both compile your code and run it.
JVM, the engine. Put it in a bigger box together with the standard library and you have the JRE, which can run programs. Put that in the biggest box, along with the tools to build programs, and you have the JDK. You install the biggest box.You will rarely meet a JRE on its own today. Since Java 11, Oracle has offered only the JDK for download, and the JDK holds everything a JRE has. A few other companies still package a JRE for computers that only need to run finished programs.
Main.java and types javac Main.java in a terminal. What happens?Installing Java 25#
To run Java on your own computer, you need a JDK. This course is written for Java 25. Several organizations build the JDK from the same open source code, called OpenJDK, so their builds behave the same way. Two common choices, both free to download and use:
- Eclipse Temurin, from the Adoptium project at adoptium.net. Choose version 25 (it is marked LTS) for your operating system.
- Oracle's own JDK, from oracle.com/java.
Builds from Amazon (Corretto), Microsoft and Azul work just as well. If one is already on your computer at version 25, keep it.
- 1Download the JDK 25 installer for your system and run it, keeping the default settings. If you prefer a package manager, one command does the same job:
winget install EclipseAdoptium.Temurin.25.JDKon Windows,brew install --cask temurin@25on a Mac with Homebrew, orsudo apt install openjdk-25-jdkon Ubuntu. - 2Open a new terminal, the window where you type commands: the Terminal app on macOS, PowerShell on Windows, or the terminal app of your Linux system. Use a fresh one, because a terminal that was already open may not see what you just installed.
- 3Type
java -versionand press Enter, then do the same withjavac -version.
On a computer with Temurin's first Java 25 release, the two commands print this (the $ marks what you type; you do not type the $ itself):
$ java -versionopenjdk version "25" 2025-09-16 LTSOpenJDK Runtime Environment Temurin-25+36 (build 25+36-LTS)OpenJDK 64-Bit Server VM Temurin-25+36 (build 25+36-LTS, mixed mode, sharing)$ javac -versionjavac 25The number to look for is the 25 near the start. Yours may say 25.0.4 or similar, because Java 25 gets small updates every three months, and the words after it name whoever built your JDK. If javac -version answers too, the compiler is installed and you are ready.
A message such as “command not found” or “is not recognized” means the terminal does not know where Java is. Close the terminal, open a new one and try again. If that does not help, the installer did not add Java to your PATH, the list of folders the terminal searches for commands; the installation guide on adoptium.net shows how to fix that on each system.
If java works but javac does not, you have a JRE rather than a JDK. If the version is lower than 25, an older Java is installed as well and the terminal finds it first.
Your first program#
Here is a complete Java program. It prints one line. Read it, then press Run.
It prints Hello, world. There are only three lines of code, and each part has a job.
void main()is the first line of a method namedmain. A method is a named block of code that does one job. When Java runs a program, it starts at the method calledmain.- The curly braces
{and}mark where the method's code begins and ends. Everything between them runs, from top to bottom. voidsays thatmainhands nothing back when it finishes. Methods that hand back a value come later, in methods.IO.println(...)prints whatever is in the parentheses and then moves to a new line.IOis a small class built into Java: a bundle of ready-made methods for simple input and output."Hello, world."is a string, a piece of text between double quotes. Java prints it exactly as written, without the quotes.- The semicolon
;ends the statement, the way a full stop ends a sentence.
Change the text between the quotes and run it again. That is your first edit to a Java program.
IO has a few more methods. IO.print prints without moving to a new line, and IO.readln waits for the person at the keyboard to type a line. You will use both in the next lesson.
The classic form: a class with a main method#
Here is the same program written the way Java programs have been written since 1996. Every later lesson in this course uses this form, so read it slowly once. Press Run: it prints the same line.
public class Main { public static void main(String[] args) { System.out.println("Hello, world."); }}Three things changed. The method now sits inside a class named Main. Its first line grew from void main() to public static void main(String[] args). And the printing line says System.out.println where the short version said IO.println.
The outer wrapper: public class Main
In this form, every piece of Java code lives inside a class. For now, treat a class as a named container for code. Here the container is named Main. The word public in front means any other code may use this class; encapsulation explains what words like it protect. The outer pair of curly braces marks where the class begins and ends.
The starting line: public static void main(String[] args)
When Java runs this program, it looks for the method named main and starts there. If you misspell it, for example as Main with a capital M, the program still compiles but refuses to start, because Java cannot find its starting point.
The words around main each have a meaning:
publicmeans code outside the class may call the method, thejavacommand included. Java insisted on it until Java 25, which is why every classicmainhas it.staticmeans the method belongs to the class itself, so Java can call it without first creating anything. Its full meaning needs objects, which come later.voidmeans the method hands nothing back, as in the short form.String[] argsholds any extra words typed after the program's name when you start it from a terminal.String[]means “a list of pieces of text”. You will rarely use it early on, but it is part of the classic line.
void and handing values back are explained in methods, and static in the static and final keywords.The printing line: System.out.println(...)
System.out is the program's standard output: normally the terminal window, or the console under each program on this page. println prints what is in the parentheses and then moves to a new line. It does the same job as IO.println, which in fact uses System.out underneath.
First, every statement ends with a semicolon. Leaving it out is the most common first error. Second, text goes between straight double quotes, "like this". Single quotes mean something else in Java (one single character, as the next lesson shows), and the curly quotes that word processors put in do not work at all.
Why the longer form is still worth knowing
The compact form is the gentler way to start, and it is real Java 25. The classic form is still the one to know well, for practical reasons:
- Nearly all the Java you will read is written this way: books, tutorials, answers online, and the code at any company that uses Java.
- Versions before Java 25 reject compact files, and many companies still run Java 17 or Java 21.
- A compact file holds one small program. As a program grows, its code is split into named classes, usually one per file, and it starts from a
mainmethod in one of them, written the classic way. - Each word in the classic line is an idea this course teaches you, from classes to
staticto the list inString[].
Key ideaBoth forms start at a method called
main. The compact form lets Java supply the class; the classic form writes it out, and it is the form almost all Java code uses, including every later lesson of this course.
Hi?Tap a line, then tap where it should go. Or use the arrows.
Printing more than one line#
A program can do more than one thing. Its statements run in order, top to bottom, exactly as written. To print three lines, you write three print statements, one after another. Run this and watch the output appear in the order the lines are written.
public class Main { public static void main(String[] args) { System.out.println("Welcome to Java."); System.out.println("This is your first program."); System.out.println("Let us begin."); }}Three statements, three lines of output, in the order you wrote them. Everything else in programming builds on this top-to-bottom flow. Later you will make a program choose between paths and repeat work, but underneath, statements still run one after another unless you say otherwise.
println moves to a new line after printing, so each call starts on a line of its own. There is also a plain print that stays on the same line, so several print calls run their text together. You will use both in variables, types, and input/output.HELLO, MY NAME IS, then Ada Lovelace, then the row of 17 stars again. The first line is done for you.Running a program on your own computer#
On this page you press Run and the two steps happen for you. On your own computer you type them in a terminal, and doing it once makes javac and java feel concrete.
- 1Make a folder for your work, and save the classic hello program in it as
Main.java. - 2Open a terminal and move into that folder with
cdfollowed by the folder's path. - 3Compile with
javac Main.java. If there are no mistakes, it prints nothing and writesMain.class, the bytecode. - 4Run with
java Main: the class name, with no.javaor.classon the end. The JVM starts, findsmain, and your output appears.
Here is the whole thing in a terminal on macOS or Linux, with ls listing the files in the folder before and after compiling (on Windows the command for that is dir):
$ lsMain.java$ javac Main.java$ lsMain.class Main.java$ java MainHello, world.javac left a new file behind, Main.class. The java command then ran it. Every Java program you will ever run, from this greeting to a program with thousands of classes, makes the same trip from source code to bytecode to the JVM.
One step: java Main.java
Since Java 11 there is a shortcut. Give the java command the source file itself, and it compiles the code in memory and runs it straight away:
$ java Main.javaHello, world.No Main.class is written this time; the bytecode lives only in memory while the program runs. This works for the compact form too: save the short void main() program as Main.java and the same command runs it. The one-step form is handy for small programs like the ones in this course. Larger programs are compiled ahead of time, usually by a build tool, which you meet in build tools.
javac Main.java and it worked. Which command now runs the program?Trying one line at a time with JShell#
Sometimes you only want to check one small thing, such as what 10 / 4 gives in Java. Writing a whole program for that is slow. The JDK comes with JShell, a tool where you type a line of Java, press Enter, and see the result at once. Start it by typing jshell in a terminal. Here is a short session:
$ jshell| Welcome to JShell -- Version 25| For an introduction type: /help intro jshell> 2 + 3$1 ==> 5 jshell> 10 / 4$2 ==> 2 jshell> IO.println("Hello from JShell")Hello from JShell jshell> /exit| Goodbyejshell>is the prompt. It means JShell is waiting for you to type.- When you type a value such as
2 + 3, JShell works it out and stores the answer in a scratch variable named$1(then$2, and so on), which you can use in later lines. - You can leave off the semicolon at the end of a line. JShell adds it for you.
- Commands for JShell itself start with a slash:
/exitleaves, and/helplists the rest.
Look again at 10 / 4: JShell answered 2, not 2.5. Java divides whole numbers that way on purpose, and operators, expressions, and casting explains why. JShell is for trying things out. Programs you want to keep belong in a .java file.
Leaving notes in your code: comments#
A comment is text the compiler ignores completely. It is there for people: to explain a tricky line, to leave a reminder, or to switch a line of code off for a while. Java has two kinds. A single-line comment starts with //, and everything from there to the end of that line is ignored. A multi-line comment starts with /* and ends with */, and everything between them is ignored, even across many lines.
public class Main { public static void main(String[] args) { // This is a single-line comment. Java ignores it. System.out.println("This line runs."); /* This is a multi-line comment. It can span several lines. None of this is run. */ System.out.println("So does this one."); }}public class Main { public static void main(String[] args) { System.out.println("one"); // System.out.println("two"); System.out.println("three"); // System.out.println("four"); /* System.out.println("five"); System.out.println("six"); */ System.out.println("seven"); }}Some print statements below are switched off with comments. Which lines does the program print?
It prints one, three and seven. The // on the line with three starts after the statement, so the statement still runs and only the rest of the line is ignored. The /* */ pair switches off both lines it wraps.
The small rules Java is strict about#
Java is strict about a handful of small, mechanical rules for characters and words, sometimes called lexical rules. Most first-week errors break one of them, so here they are in one place.
- Every statement ends with a semicolon
;. A missing semicolon is the most common beginner error of all. - Java is case-sensitive:
countandCountare two different names.mainmust be all lowercase, and so mustvoidandclass. - Spaces, tabs and blank lines mostly do not matter, so you are free to indent your code to make it readable. Java goes by the braces, not the indentation.
- Keywords such as
class,public,staticandvoidhave fixed meanings, so you cannot use them as names of your own. - A name you choose, called an identifier, must start with a letter, an underscore
_or a dollar sign$, and may contain digits after that.score1is fine;1scoreis not.
When you break one of these rules, javac stops and tells you where. Here is what it prints for the hello program with the semicolon missing from line 3:
$ javac Main.javaMain.java:3: error: ';' expected System.out.println("Hello, world.") ^1 errorRead it from the left: the file, the line number, then what Java expected to find. The ^ points at the spot where it noticed the problem. No Main.class is written when there is an error, so there is nothing to run until you fix it. Reading messages like this one is a skill of its own, and reading errors and debugging builds it.
;, a capital letter where you meant a small one, a missing closing brace, or a name that clashes with a keyword. Most of the time the fix is one of those.Try it
Answer each one in your head or on paper first, then open the answer.
.java file and a running program, and say which tool does each.println statements, what would change in the output?javac Main.java and see error: ';' expected. What is wrong, and is there a Main.class file in the folder now?total, 2nd, _count, class, userName? Say why for each.// comment and a /* ... */ comment? When would you use each?Practice
Work through these on your own. Each one builds on this page.
java -version and javac -version. Then save the classic hello program as Main.java, compile it with javac and run it with java Main. Delete Main.class and run the program again with java Main.java. Which of the two ways leaves a .class file behind?7 + 5, 7 / 2, 7.0 / 2 and "Java" + 25, one at a time. Write down what JShell shows for each. Which one surprised you, and what do you think the .0 changed?.java file to output on the screen, labelling where javac, the .class file and the JVM each appear. Then add a second, shorter path for java Main.java.Further reading#
- Getting started with Java on dev.java, the official Java learning site: getting a JDK, compiling and running
- JShell, the Java shell tool, on dev.java
- JEP 512: Compact Source Files and Instance Main Methods, the proposal that brought void main() to Java 25
- The IO class in the Java SE 25 API documentation
Short guides on this topic
Quick answers to what people search for about it, with code to run.
Spotted a mistake, or a part that did not make sense? Report it by email