Hello, Java

The Course
•
4 min read

A program is a set of instructions a computer follows. Java is a programming language for writing those instructions. You do not need to know Python or JavaScript: despite their names, Java and JavaScript are different languages.

Start by reading the first example from top to bottom, then press Run. The editor runs in this browser; no account or installation is needed. The output panel shows what the program printed. Reset restores the original example, so you can experiment without worrying about losing the supplied version.

A small program with a reusable frame

For now, treat the outer lines as a frame. public class Main names this program’s class, and the file is called Main.java. main is where this program starts. String[] args is part of that entry-point declaration; leave it in place. We will explain arrays and methods later. You do not have to memorize the whole frame before you can use it.

The indented statements inside main are the instructions we are changing. System.out.println prints a value and moves to a new line. Text goes inside straight double quotes. A semicolon finishes the statement. Braces mark the start and end of a block; indentation helps a person see which instructions belong there. We use four spaces per level and one statement per line.

Predict before running

Write the two output lines on paper. Punctuation inside the quotes is part of the message; quotes and semicolons outside the message are Java syntax.

Two instructions, in order Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
        System.out.println("I can change this program.");
    }
}
Check your prediction
Hello, Java!
I can change this program.

Execution reaches the first print before the second. Swap the two statements and predict the new order. Changing the message changes data; moving the statement changes the sequence of work.

print versus println Java
public class Main {
    public static void main(String[] args) {
        System.out.print("Study ");
        System.out.println("Java");
        System.out.println("Day 1");
    }
}
Check your prediction
Study Java
Day 1

print leaves the next output on the same line. The space after Study is inside the string. Remove it, run again, and explain why the two words touch. Blank lines between Java statements do not print blank lines.

Compiler error: a missing semicolon Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello")
    }
}
Read the diagnostic

The compiler reports that a semicolon is expected. It checks the source before this program runs. Add the missing semicolon and run again. Start with the first diagnostic: a small missing token can cause later messages too.

A line beginning with // is a comment: a note for the reader that Java does not run. The workspace below uses one to suggest where to write. Replace the comment with your statements, or leave it and write on the next line. Code written after // on that same line is also ignored.

Your turn

Exercise

Introduce yourself

Print your name, a topic you want to learn and a short goal on three separate lines. Choose your own messages. The example solution uses Ada, Java and Write a program; your own text can differ.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        // Write three println statements here.
    }
}
Hint 1: the idea

Each message is a separate instruction, and println ends a line.

Hint 2: a first step

Keep the frame unchanged. Start with one quoted message and get it running before adding the next two.

Show one solution
public class Main {
    public static void main(String[] args) {
        System.out.println("Ada");
        System.out.println("Java");
        System.out.println("Write a program");
    }
}
Ada
Java
Write a program

Change it and explain

Change one thing at a time: a message, its punctuation, then the order. Deliberately remove a closing quote and read the first error; restore it before continuing. Tell someone the difference between source code and output without pointing at the screen. If you can only recognize the example, close the solution and write it again from an empty main body.

Each run starts fresh; one editor does not define names for another. You do not need to install anything for this course. The playground supports the Java features used here; it does not include every feature of a full Java installation. If you see an unsupported-feature message while experimenting, that is a playground limit. Day 7 offers an optional guide to running Java on your computer.

Was this article helpful?

Your response is saved on this device.