Skip to content
KALANBLON
Show contents

Chapter 2 of 4 · 15 min

Anatomy of a Java program

By the end of this chapter, you'll be able to

  • Explain the role of the main method
  • Display text with IO.println and IO.print
  • Put text and values together with the + operator
  • Recognise the classic form of a Java program with a class

The skeleton

// Every program starts with the main method
void main() {
    IO.println("Hi!");              // displays, then moves to a new line
    IO.print("On the same ");       // displays without moving to a new line
    IO.println("line.");
}
Hi!
On the same line.
  • void main() declares the main method: that's where the program starts. void means it returns no value. The curly braces { } mark out its content.
  • Each instruction ends with a semicolon ;.
  • IO.println(...) displays a value then moves to a new line; IO.print(...) displays without moving to a new line. The IO class, introduced with Java 25, groups simple console input and output.
  • // starts a comment until the end of the line, and /* … */ a comment over several lines. Java ignores them.

Indentation isn't mandatory, but everyone uses it: it makes the structure visible at a glance.

Putting text together with +

Between two texts, + concatenates them: it sticks them end to end. Between a text and a number, the number is converted into text:

void main() {
    int apples = 6;
    IO.println("I have " + apples + " apples.");
    IO.println("Total: " + (apples * 250) + " FCFA");
    IO.println("Line 1\nLine 2\twith a tab");
}
I have 6 apples.
Total: 1500 FCFA
Line 1
Line 2	with a tab

\n moves to a new line and \t inserts a tab: these are escape sequences. To write a double quote inside a text, use \".

End of main: the program finishes.

Step 6 of 6
Text description of the animation
  1. When the program is launched, the JVM calls main.
  2. We store 12 in the variable score. We'll look at variables in detail in the next chapter.
  3. print displays the text and stays on the same line.
  4. The calculation is done first, then println displays 24 and moves to a new line.
  5. The next line is displayed underneath.
  6. End of main: the program finishes.

The classic form

Before Java 25, and in most existing projects, the same program is written inside a class, with a longer main method, and System.out.println instead of IO.println:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Kalanblon!");
    }
}
Hello, Kalanblon!

You'll understand every word of this form in the course on objects: class, public, static, String[] args. Until then, we use the simplified form, which works in exactly the same way. Careful: with the classic form and javac, the file must have the name of the public class (Hello.java).

Your turn

Worked exercise

Worked exercise: the cinema ticket

A cinema ticket costs €9, and you buy 3. Write a program that stores these two values in int variables, then displays exactly:

Tickets: 3
Unit price: 9 €
Total: 27 €
Hint 1
Declare int tickets = 3; and int price = 9;, then put the texts together with +.
Hint 2
For the total, put the calculation between parentheses: "Total: " + (tickets * price) + " €".
Show the solution and common mistakes
void main() {
    int tickets = 3;
    int price = 9;
    IO.println("Tickets: " + tickets);
    IO.println("Unit price: " + price + " €");
    IO.println("Total: " + (tickets * price) + " €");
}
Tickets: 3
Unit price: 9 €
Total: 27 €

Common mistakes:

  • Forgetting the parentheses around tickets * price: here the result would still be correct, because * is computed before +. But with an addition, "Total: " + tickets + price would display 39. The parentheses make the intention clear.
  • Forgetting the space in " €": the symbol would be stuck to the number.

Summary

  1. 1The program starts in main; each instruction ends with ;.
  2. 2IO.println displays and moves to a new line, IO.print stays on the same line.
  3. 3+ concatenates as soon as a text appears, reading from left to right: put calculations between parentheses.
  4. 4\n moves to a new line, \t inserts a tab.
  5. 5The classic form uses a class, public static void main(String[] args) and System.out.println.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.