Learn Java

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 Updated 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.

By the end of this lesson
  • Describe the journey from a .java file to a running program, and say what javac, 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 Main form.
  • Run a program from a terminal with javac and then java, or in one step with java Main.java.
  • Try single lines of Java in JShell.
  • Leave comments in your code, and spot the small slips behind most first errors.
From before
This is the first lesson, so there is nothing to recall. It assumes no programming experience: if you can create a file and type into it, you have what you need. Every program on this page also runs here in your browser, so you can follow along before you install anything.

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.

  1. 1You compile the file. A tool called javac, the Java compiler, reads your .java file, 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.
  2. 2You run the program. The java command starts the JVM, short for Java Virtual Machine, and hands it the .class file. 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 an imaginary computer?
Virtualhere means “made of software”. The JVM is a program that behaves like a computer with its own small set of instructions, and bytecode is written in those instructions. While your program runs, the JVM turns them into the real instructions of the processor underneath.

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 idea

You 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.

Coming from Python or C?
Python normally runs your .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.
Name
What is inside it, and when you need it
JVM
The engine alone. It runs bytecode. It comes inside the JRE and the JDK; you never install it by itself.
JRE
The JVM and the standard library. Enough to run a program someone else compiled. No compiler.
JDK
The JRE and the developer tools (javac and others). Enough to write, compile and run. Install this one.
Key idea

The 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.

A quick way to remember it
Think of boxes inside boxes. The smallest box is the 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.

Quick check
Someone installs a JRE, writes 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.

  1. 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.JDK on Windows, brew install --cask temurin@25 on a Mac with Homebrew, or sudo apt install openjdk-25-jdk on Ubuntu.
  2. 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.
  3. 3Type java -version and press Enter, then do the same with javac -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):

Terminal
read along
1$ java -version
2openjdk version "25" 2025-09-16 LTS
3OpenJDK Runtime Environment Temurin-25+36 (build 25+36-LTS)
4OpenJDK 64-Bit Server VM Temurin-25+36 (build 25+36-LTS, mixed mode, sharing)
5$ javac -version
6javac 25

The 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.

If the terminal cannot find java

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.

Something to type your code in
Any plain text editor works, such as Notepad on Windows. Avoid word processors such as Word, which save formatting that Java cannot read. Most Java programmers use a code editor or an IDE (integrated development environment) such as IntelliJ IDEA or Visual Studio Code, which color your code, point out mistakes as you type and run a program with one click. You meet one properly in annotations, docs and the IDE.

Your first program#

Here is a complete Java program. It prints one line. Read it, then press Run.

Main.java
1void main() {
2 IO.println("Hello, world.");
3}
Prints
›Hello, world.

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 named main. A method is a named block of code that does one job. When Java runs a program, it starts at the method called main.
  • The curly braces { and }mark where the method's code begins and ends. Everything between them runs, from top to bottom.
  • void says that main hands 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. IO is 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.

Main.java
1public class Main {
2 public static void main(String[] args) {
3 System.out.println("Hello, world.");
4 }
5}
Prints
›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.

Classes get a lesson of their own
Here a class is only a container for code. What a class really is, and why Java is built around classes, is a large part of this course, starting with creating classes and objects. Until then, copy this wrapper as it is.

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:

  • public means code outside the class may call the method, the java command included. Java insisted on it until Java 25, which is why every classic main has it.
  • static means the method belongs to the class itself, so Java can call it without first creating anything. Its full meaning needs objects, which come later.
  • void means 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.
You do not need to understand static and void yet
Two of those words point at ideas the course builds later. For now, read the whole line as a fixed phrase that means “start here”. 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.

Two beginner traps in this one line

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 main method in one of them, written the classic way.
  • Each word in the classic line is an idea this course teaches you, from classes to static to the list in String[].
Key idea

Both 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.

Quick check
Which of these is a complete Java 25 program that prints Hi?
Put the lines in orderTea.java
Put the lines back in order so the program prints the three steps for making tea, in the right order. Watch the braces: the class opens first and closes last.

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.

Main.java
1public class Main {
2 public static void main(String[] args) {
3 System.out.println("Welcome to Java.");
4 System.out.println("This is your first program.");
5 System.out.println("Let us begin.");
6 }
7}
Prints
›Welcome to Java.
›This is your first program.
›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 and print
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.
ExerciseBadge.java
Print a name badge. Finish the program so that it prints four lines: a row of 17 stars, then 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.

  1. 1Make a folder for your work, and save the classic hello program in it as Main.java.
  2. 2Open a terminal and move into that folder with cd followed by the folder's path.
  3. 3Compile with javac Main.java. If there are no mistakes, it prints nothing and writes Main.class, the bytecode.
  4. 4Run with java Main: the class name, with no .java or .class on the end. The JVM starts, finds main, 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):

Terminal
read along
1$ ls
2Main.java
3$ javac Main.java
4$ ls
5Main.class Main.java
6$ java Main
7Hello, 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:

Terminal
read along
1$ java Main.java
2Hello, 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.

Quick check
You ran 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:

Terminal
read along
1$ jshell
2| Welcome to JShell -- Version 25
3| For an introduction type: /help intro
4
5jshell> 2 + 3
6$1 ==> 5
7
8jshell> 10 / 4
9$2 ==> 2
10
11jshell> IO.println("Hello from JShell")
12Hello from JShell
13
14jshell> /exit
15| Goodbye
  • jshell> 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: /exit leaves, and /help lists 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.

Main.java
1public class Main {
2 public static void main(String[] args) {
3 // This is a single-line comment. Java ignores it.
4 System.out.println("This line runs.");
5
6 /* This is a multi-line comment.
7 It can span several lines.
8 None of this is run. */
9 System.out.println("So does this one.");
10 }
11}
Prints
›This line runs.
›So does this one.
Predict the outputComments.java
1public class Main {
2 public static void main(String[] args) {
3 System.out.println("one");
4 // System.out.println("two");
5 System.out.println("three"); // System.out.println("four");
6 /* System.out.println("five");
7 System.out.println("six"); */
8 System.out.println("seven");
9 }
10}

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.

Comments are for the reader
Good comments explain why a piece of code exists, not what each line does (the code already says that). Use them sparingly and keep them true. A comment that no longer matches the code, because the code changed and the comment did not, is worse than no comment.

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: count and Count are two different names. main must be all lowercase, and so must void and class.
  • 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, static and void have 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. score1 is fine; 1score is 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:

Terminal
read along
1$ javac Main.java
2Main.java:3: error: ';' expected
3 System.out.println("Hello, world.")
4 ^
51 error

Read 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.

Check these first
When the compiler complains in your first weeks, look for a missing ;, 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.

1
Name the two steps Java takes between your .java file and a running program, and say which tool does each.
2
In your own words, what does “write once, run anywhere” mean, and which part of the system makes it possible?
3
The greeting program prints three lines. If you swapped the first and last println statements, what would change in the output?
4
You type javac Main.java and see error: ';' expected. What is wrong, and is there a Main.class file in the folder now?
5
Which of these names are legal Java identifiers: total, 2nd, _count, class, userName? Say why for each.
6
What is the difference between a // comment and a /* ... */ comment? When would you use each?

Practice

Work through these on your own. Each one builds on this page.

1
Install a JDK 25 on your computer and check it with 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?
2
Write a program from an empty file that prints exactly four lines: a greeting, your favorite food, your favorite city, and a goodbye. Write it once as a compact source file and once in the classic form, and check that both print the same thing.
3
Open JShell and type 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?
4
In a few sentences, explain to a friend who has never programmed what bytecode is, and why Java compiles to bytecode instead of to the processor's own machine code.
5
On paper, draw the journey from a .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#

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