Chapter 1 · Foundations
Hello, Java
Step 1 of 13 · Concept
Welcome
A program is a list of instructions for a computer. In this course you write them in Java.
In this lesson you will:
- run a real Java program,
- see what happens when it runs,
- write two programs of your own.
Step 2 of 13 · Demo
Run your first program
This program prints one line of text.
Press Run. The output's first line shows the commands Run typed. Under it is what the program printed.
Step 3 of 13 · Concept
What Run did
Run typed two commands for you. You see them on the output's first line.
| command | what it does |
|---|---|
javac | the compiler: checks Hello.java and turns it into Hello.class |
java | starts the Java virtual machine, which loads Hello.class and runs its main |
These are the real tools. Professional Java programmers use the same two.
Step 4 of 13 · Quiz
Who reads your file?
Which tool reads your file Hello.java?
Step 5 of 13 · Concept
The shape of a program
A Java program has this shape:
- a class:
public class Hello { ... } - inside it, a method named
main: Java starts here - inside
main, statements: they run from top to bottom, and each ends with;
Step 6 of 13 · Quiz
Same name, same file
A file is named Greeting.java. Which first line fits it?
Step 7 of 13 · Concept
println and print
| statement | prints the text, then ... |
|---|---|
System.out.println("Hi"); | ends the line: the next output starts on a new line |
System.out.print("Hi"); | stays on the line: the next output comes right after it |
System.out.println(); | prints nothing, and ends the line |
The ln in println stands for line.
Step 8 of 13 · Quiz
Predict the output
What does this print?
System.out.print("I am ");
System.out.println("learning.");
System.out.println("Bye.");
Step 9 of 13 · Exercise
Your turn: two lines
Time to write code. Change one line, then add one.
Change the program so it prints exactly these two lines:
Hello, Java!
I am learning.
You need two statements: one for each line.
Change the text in the quotes to Hello, Java!. Then copy the whole println line and change its text.
System.out.println("Hello, Java!");
System.out.println("I am learning.");
Step 10 of 13 · Exercise
Your turn: write main
Here is the shape again. Use it.
public class Poem {
public static void main(String[] args) {
// statements go here
}
}
The class Poem has no main, so it cannot run. Write main so the program prints exactly:
A class, and in it main:
Java starts there
and reads from the top.
Replace the comment with the line public static void main(String[] args) { and a closing }.
Inside main, write three System.out.println(...); statements, one per line of the poem.
Step 11 of 13 · Check yourself
Check yourself: about me
No shape this time. You know it.
Write the whole program in AboutMe.java: the class, main and the statements. It must print exactly:
About me
Name: Ada Lovelace
Likes: numbers, machines and poems
Lesson: 1, done!
The class is named after the file: public class AboutMe { ... }. main goes inside it.
Step 12 of 13 · Challenge · optional
Challenge: a rocket
Optional. It earns the lesson's third star.
Print this rocket, exactly:
/\
/ \
| |
/____\
Run it first. javac says unclosed string literal. Inside quotes, \ starts a special code: \" is a quote mark in the text, not its end.
To print one \, write two: "\\". So " /\\" prints /\.
Step 13 of 13 · Recap
Recap
- A program is a class with a method
main. Java starts atmain. - Statements run from top to bottom. Each one ends with
;. - Run =
javac(check and compile) +java(run). printlnends the line.printdoes not.- The file name matches the class name, capital letters included.
Next: lesson 2 looks inside the computer: where your program lives while it runs.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
public class Poem {
// write the method main here
}
// write the class AboutMe here
public class Rocket {
public static void main(String[] args) {
System.out.println(" /\");
}
}