A computer's processor carries out very small instructions stored as numbers: fetch this value, add these two, jump back if the result is zero. Nobody writes a banking app or a flight tracker that way. People write in a language they can read, and a tool turns that text into something the machine can carry out. Every page after this one assumes you can keep those two sides apart, so this section starts there.
A computer's chip only follows instructions written as long strings of numbers. People can't sensibly write that way. So we write in a language made for people, and a tool rewrites it into the chip's numbers.
That people-friendly language is a programming language. Java is
the one this course teaches. Unlike English, it has strict rules: each
word and symbol means exactly one thing. The instructions you type in
Java are called source code. They are plain text, saved in a file
whose name ends in .java, like Hello.java.
The tool that rewrites your text is the compiler. It reads your
file, checks every rule, and writes out a new file the computer can run.
For Hello.java, that new file is called Hello.class. The finished,
runnable result is the program: a set of instructions the computer
can carry out.
Two habits follow from this. First, you only ever change the .java
file, the one a person wrote. Hello.class is the tool's output, so
after a change you translate again. Second, the compiler reads the whole
file before anything happens. One broken rule anywhere, and it stops
with a message. Nothing runs until you fix the text.
A program is a set of instructions a computer can carry out. A
programming language is a notation for writing those instructions
with fixed rules: every word and symbol has one defined meaning, so a
tool can process the text without guessing what the author meant. Java
is a programming language. The text a person writes in it is source
code, saved in ordinary text files whose names end in .java, such as
Hello.java.
The processor cannot carry out source code directly. A compiler is a
program that reads source code, checks it against the language's rules,
and translates it into a form that can run. The Java compiler reads
Hello.java and writes a new file, Hello.class, which Java's runtime
loads and runs. Chapter 1 opens up both of those steps. Two facts from
this picture carry the whole course:
- Source code and the translated program are different files. You edit the source and never the translated file; to change the program, you change the source and translate it again.
- The compiler checks the entire file before anything runs. If one rule is broken anywhere, it reports an error and produces nothing new to run, so not even the first line's work happens.
Written by you, or made by a tool?
Sort each card: is it source code a person writes, or something a tool produced from it?
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
.java files and the readable IO.println line are source code. Hello.class and Score.class are what the compiler writes, and that is why they look like scrambled symbols in a text editor.Worked example
Which file do you open to change the price?
-
1
A bakery's price-list program was built from
PriceList.java. In the same place on the disk there is now a second file,PriceList.class. The owner wants bread to show as 4 instead of 3. -
2
The price lives in the text a person typed, which is
PriceList.java. That is the source code, and it's the file to open in an editor. -
3
PriceList.classis the compiler's output. Opened in a text editor it shows mostly unreadable symbols, because it was written for Java to run, not for a person to read. Editing it by hand would, at best, do nothing useful. -
4
After changing the 3 to a 4 in
PriceList.javaand saving, the owner runs the compiler again. It writes a freshPriceList.class, and the new price appears. Change the source, then translate again.
Worked example
Why the first line didn't print either
-
1
A ten-line program has a broken rule on line 7. The student expected lines 1 to 6 to do their work and the program to stop at line 7.
-
2
That isn't how a compiler works. It reads all ten lines first and checks every rule before it writes anything.
-
3
On line 7 it finds the broken rule, reports it, and writes no new runnable file. With no file to run, nothing runs: line 1 does nothing either.
-
4
So "nothing printed at all" plus an error message points to a problem in the text, found before running. Read the message, fix the text, and translate again.
Check your understanding (counts toward your progress)
You typed some Java into a file called Receipt.java. What is that file?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Nothing runs from Receipt.java itself. The compiler makes a separate file from it, and that is what runs.
Number instructions only exist after translation. Receipt.java is text you could read aloud.
It's a single file. The .java ending marks it as Java source code.
Why
A .java file holds source code: readable text a person typed. The compiler checks it and translates it into a separate file, and that file is what runs.
Your program prints Seats: 30 but should print Seats: 32. Put the steps to fix it in order. One step doesn't belong.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Edit the source, save, compile again, run the new result. Seats.class is the compiler's output; editing it is the step that doesn't belong.
Correct answer
Also accepted:
Why
Which file is meant for a person to edit?
Why can't the processor carry out Hello.java as it is?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Size isn't the issue. A one-line .java file is just as unusable to a processor.
Any plain text editor opens a .java file. People reading it is fine; the processor understanding it is the problem.
Nothing is downloaded when a program runs. Translation happens on your own machine, before running.
Why
A processor carries out small instructions stored as numbers. Java is text written for people, so a compiler must translate it before anything can run.
What kind of instruction does a processor understand?
Sort each item: did a person write it, or did a tool make it from what a person wrote?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Files ending in .java and readable Java lines are source code. Files ending in .class, which look like random symbols in an editor, are what the compiler wrote.
Correct answer
Also accepted:
Why
Could you open it and read it like a sentence?
Practice: Source code and the compiler (5 problems · doesn’t affect your progress)
Which pair describes the two kinds of file in a Java program's folder?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That's reversed. People write the .java text; the compiler writes .class.
Nobody types a .class file. It's the compiler's output.
The compiler needs something to read, and that's the .java file a person wrote.
Why
People write .java source code. The compiler reads it and writes a .class file, which is what runs.
.java files you write; .class files the compiler makes
Sort each job: is it done by you, or by the compiler?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
You write and save the source. The compiler checks it, reports any broken rule, and writes the .class file.
Correct answer
Also accepted:
Why
Your weather program says Rain but should say Sun. Put the steps in order.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Edit the source, save, compile, run. Skipping the compile step runs yesterday's translation.
Correct answer
Also accepted:
Why
Running before compiling would run the old translation.
Hana deletes Clock.class by mistake but still has Clock.java. What has she lost?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The instructions live in the source. Clock.class was only a translation of them.
The .class file is a full translation of the whole source, not half of anything.
Deleting one file leaves the other untouched.
Why
The .class file is made from the source, so the source is what matters. Compile again and it's back.
Nothing lasting: compiling Clock.java remakes it
Hana's program has a broken rule on its last line. She compiles, then immediately runs Clock.class from last week. What happens?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The old Clock.class is still a complete, working translation. It knows nothing about the new source.
The failed compile produced nothing new, so there's no partial program to stop.
Compilers never repair source code.
Why
A refused compile leaves the old .class file in place. Running it runs the old version, which is why you should always check the compile succeeded before trusting what runs.
A failed compile produces nothing. What's still in the folder?
Last week's program, since the failed compile wrote nothing new
Reading your first Java program
You don't need to understand every symbol yet. Read a short program the way you'd read a recipe in a language you half know: find the parts that carry meaning, and go top to bottom.
Look at the program below. Its first line, void main() {, means
"the program starts here." The final } means "the program ends here."
Chapter 1 explains those two lines properly.
Everything in between is a list of instructions. Each one ends with a
semicolon (;), the way a sentence ends with a full stop. The computer
does them in order: the first, then the second, then the third. Then it
stops.
IO.println means "print a line." Whatever you put inside its round
brackets appears on the screen. Then the next print starts on a fresh
line, as if someone pressed Enter.
Quotes decide what gets printed. With double quotes around it, the text
is printed letter for letter. Without quotes, as in 3 + 4, Java works
out the answer first and prints 7.
Capital letters matter in Java, which programmers call being
case-sensitive. IO.println works. Write io.println with small
letters, or IO.Println with a capital P, and Java treats it as a word
it has never seen. It refuses the whole file.
The program below is a complete Java file. The first line,
void main() {, marks where the program starts, and the closing } on
the last line marks where it ends; Chapter 1 explains that pair. The
lines between them are instructions, and each one ends with a semicolon
(;). They run once each, in order, from top to bottom. After the last
one, the program ends.
IO.println(...) is the instruction that prints. Whatever sits between
its parentheses is written to the screen, followed by a line break, so
the next print starts on a new line. Text in double quotes is printed
exactly as written, spaces and punctuation included. Anything else
between the parentheses is worked out first and its result printed:
IO.println(3 + 4) prints 7, while IO.println("3 + 4") prints the
five characters 3 + 4.
Java is case-sensitive: names that differ only in capital versus
small letters are different names. IO.println is the name Java
provides. io.println and IO.Println are names Java has never heard
of, and the compiler refuses the file.
void main() {
IO.println("Hello!");
IO.println("Welcome to Java.");
IO.println(3 + 4);
}
Hello!
Welcome to Java.
7
Worked example
Reading it line by line
-
1
IO.println("Hello!");prints the quoted text,Hello!, and ends the line. The;closes the instruction. -
2
IO.println("Welcome to Java.");runs next, because instructions run top to bottom. It printsWelcome to Java.on the second line, full stop included, since everything inside the quotes is copied as written. -
3
IO.println(3 + 4);has no quotes, so Java adds 3 and 4 first and prints the result,7, on the third line. -
4
After the third instruction there is nothing left, so the program ends. Three printing instructions, three lines of output.
Predict before you peek
Type exactly what the program prints, one line per line. The answer was produced by running the program on a real Java 25 installation.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
40 + 25 is printed as those seven characters. The third has no quotes, so Java adds 40 and 25 and prints 65.Worked example
One pair of quotes changes the output
-
1
Compare
IO.println("10 - 4");withIO.println(10 - 4);. The only difference is the pair of double quotes. -
2
With quotes,
10 - 4is text. Java does not look inside it; it prints the six characters10 - 4as they are. -
3
Without quotes,
10 - 4is a calculation. Java works it out and prints6. -
4
When you predict output, ask "is this in quotes?" before anything else. That one question settles most early predictions.
Check your understanding (counts toward your progress)
Sort each instruction by what it prints.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
5 + 3 and 10 - 2 without quotes are worked out to 8, and "8" is the text 8. The quoted calculations are printed character for character.
Correct answer
Also accepted:
Why
Without quotes, Java works the calculation out. With quotes, it copies the characters.
Fill the blank so the program prints Match starts now.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
IO.println prints its text and ends the line. Every letter of println is small; Println would be a different name.
Correct answer
Also accepted:
Why
It's the name that means "print a line." All small letters.
Put the lines in order so the program prints BOARDING PASS, then Seat 14C, then Have a good flight.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Instructions run top to bottom, so the output comes out in the same order as the lines. To print the heading first, put its line first.
Correct answer
Also accepted:
Why
The line nearest the top prints first.
What does this program print? Watch the quotes.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The first line is in quotes, so it prints the characters 12 + 8 as written. The second has no quotes, so Java adds 12 and 8 and prints 20.
Correct answer
Also accepted:
Why
Which line has quotes, and which doesn't?
Practice: Read a Java program (5 problems · doesn’t affect your progress)
In the program below the line void main() {, which lines do the printing?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That line marks where the program starts. It prints nothing.
The } marks where the program ends.
Opening a file in an editor shows its text. Running it is what makes it print.
Why
The lines between the braces are the instructions, and each IO.println prints one line.
The IO.println(...) lines between { and }
A one-line greeting program. What does it print?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
One printing instruction, one line of output: the quoted text as written.
Correct answer
Also accepted:
Why
Good morning
A fruit-stall program prints two labels and a sum. What's the output?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The first two lines are quoted text. The third has no quotes, so Java adds 6 and 9 and prints 15.
Correct answer
Also accepted:
Why
Which line has no quotes?
Apples: 6 ⏎ Pears: 9 ⏎ 15
Fill the blank so the program's second line prints the number 30, worked out by adding 18 and 12.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
18 + 12 without quotes is worked out, so the line prints 30. With quotes it would print 18 + 12.
Correct answer
Also accepted:
Why
Leave the quotes off, or Java will print the characters instead of adding.
18 + 12
Two lines look almost the same. Type exactly what this prints.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The first line prints the characters 5 - 2. The second works out 5 − 2 and prints 3. The third prints the text 3. The last two outputs look identical, though only one was calculated.
Correct answer
Also accepted:
Why
Ask of each line: is it in quotes?
5 - 2 ⏎ 3 ⏎ 3
Two first-day mistakes: the semicolon and the capital letter
Two mistakes cause most of the errors people hit on their first day. Both are caught by the compiler, so the program never runs. Every program on this page, including these two broken ones, was given to a real Java compiler when the course was built, and these are the errors it really reported.
A missing semicolon. The compiler's message is ';' expected, and it
points at the end of line 2:
void main() {
IO.println("Hello!")
IO.println("Bye!");
}
A small letter where a capital belongs. The compiler's message is
cannot find symbol: it knows IO, but nothing called io.
void main() {
io.println("Hello!");
}
Bug hunt: the program that won't compile
Fix the 1 mistake so this program compiles and prints both lines.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
; after the closing bracket.Worked example
Fixing a program that won't compile
-
1
A student's receipt program prints nothing at all, only an error. The file has
IO.println("RECEIPT")on line 2 andIO.println("--------");on line 3. -
2
Not even the first line printed. That means the compiler stopped the file before it could run: the problem is in the text, not in what the program did.
-
3
Check how each instruction ends. Line 3 ends in
;. Line 2 ends in)with no semicolon, so Java can't tell where the first instruction stops. -
4
Add the
;, compile again, and both lines print. When a program shows an error and prints nothing, look at the text before anything else.
Professional habit: when the compiler reports several errors, fix the first one and compile again. One missing semicolon can confuse the compiler about the lines after it, and the later messages often vanish once the first is fixed.
Where this goes next
Chapter 1 §5 has you type a program like these into a file of your
own. You run it by typing one line, java Hello.java, which translates
and runs in a single step. Chapter 1 §6 splits that into two separate
commands and shows Hello.class appearing. Chapter 1 §9 teaches you to
read the ';' expected and cannot find symbol messages you just met,
line number included.
Check your understanding (counts toward your progress)
The compiler reports cannot find symbol for the line io.println("Hi");. What is it telling you?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The compiler never checks the spelling of text inside quotes. Hi is printed as written.
The ; is there. A missing semicolon gives the message ';' expected, a different one.
Nothing ran. A compiler message means the file was refused before running.
Why
cannot find symbol means the compiler met a name it has never heard of. Because Java is case-sensitive, io is not IO.
Compare the name in the line with the name Java provides, letter by letter.
This program has 2 mistakes. Fix both so it compiles and prints Seats left: and then 12.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Line 2 needs its closing ;. Line 3 has a capital P in Println; the name Java provides is println, all small letters.
Correct answer
Also accepted:
Why
One mistake is punctuation; the other is a single capital letter.
Ben's two-line program prints nothing at all and shows the message ';' expected. What should he do first?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
The file hasn't changed, so the compiler will refuse it in exactly the same way.
Nothing printed because nothing ran. The first line may be fine; the message points to the real problem.
The compiler isn't stuck. It found a broken rule in the text, and only fixing the text helps.
Why
The message says a ; is missing and points at the line. Nothing ran because the compiler refused the file, so the fix is in the text.
The message names the missing character. Where would it go?
The compiler reports four errors. Line 2 is missing its ;, and the other three messages point at lines below it. Put a sensible order of work.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Fix the first error, then compile again before touching anything else. One missing ; can make the compiler misread the lines after it, so the three later messages often vanish once line 2 is fixed.
Correct answer
Also accepted:
Why
Can one missing symbol confuse the compiler about the lines after it?
Practice: First-day mistakes (5 problems · doesn’t affect your progress)
A program has one missing semicolon. What happens when you compile it?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Nothing runs from a refused file, so no lines run at all.
Java never edits your source for you.
Output layout isn't affected. The file doesn't get as far as running.
Why
The compiler checks the whole file first. A missing ; breaks a rule, so it reports ';' expected and nothing runs.
It refuses the file and says where ; is expected
Fix the 1 mistake so this program compiles.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Add ; after the closing bracket so the instruction is complete.
Correct answer
Also accepted:
Why
This program gives cannot find symbol. Click the 1 broken line.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Line 4 writes Io with a small o. Java provides IO, both capitals, and treats Io as an unknown name.
Correct answer
Also accepted:
Why
Look at the capitals before the dot on every line.
Fix the 1 mistake so the program compiles and prints Order 7 is ready.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The ( after println is never closed. Add ) after the closing quote, before the ;.
Correct answer
Also accepted:
Why
Every ( needs a ).
This program has 2 mistakes. Fix both so it compiles and prints its two lines.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Line 2 needs capital IO and a closing ;. Both mistakes sit on one line, which is why reading a line fully matters even after you've found one problem on it.
Correct answer
Also accepted:
Why
Both mistakes are on the same line.