E ExamMaster

Object-Oriented Programming through Java · Language Basics

Java Program Structure

In Object-Oriented Programming through Java because every later listing lives in a class with a main door — tokens, statements, comments, escapes, arguments and typed input are…

A Java program you can run lives in a class. The machine starts at a method named main — the door into that class. This lesson walks the file: tokens, statements, comments, escape sequences, words typed before the program starts, and words typed while it runs. Still Meera's ticket; now you can print it.

  • Object-Oriented Programming through Java
  • Easy level
  • 6 concepts

1A class is the box; main is the door

A Java program you run lives in a class, and the machine starts at a method named main. Think of the class as the named box and main as the door.

Until later units you will put almost every line inside that door. The class name and the file name should match: TicketPrinter lives in TicketPrinter.java.

Figure. The outer box is the class. The inner box is main — the only place the machine starts. Lines you want to run on launch go inside that inner box.

How it works

  1. A class is the box; main is the doorThe class is the named box. main is the door the machine starts at.

Smallest runnable ticket printer

public class TicketPrinter {
    public static void main(String[] args) {
        System.out.println("Seat A12");
    }
}

Coding lab. Print Meera's seat runs in the app, with checks on your output.

2The compiler reads tokens, not a story

The compiler does not read the file as a story. It splits the text into tokens: words, numbers, symbols, and quoted strings.

class, TicketPrinter, and { are separate tokens. A misspelled token is not almost right — it is a different token, or an error.

No diagram — the idea is carried by the prose or the Java listing.

Tokens in the ticket printer
TokenKindRole
classkeywordStarts a class
TicketPrinternameThe class name
"Seat A12"stringText to print
;symbolEnds a statement

3A statement is one instruction

A statement is one instruction the machine carries out. In Java most statements end with a semicolon.

A missing semicolon is not a style issue. The compiler cannot see where the instruction ended, so it stops.

Figure. A statement is one instruction. These two print lines are two statements, each ended by a semicolon. A missing semicolon is not style — the compiler cannot see where the instruction ended.

How it works

  1. A statement is one instructionOne instruction, then a semicolon. The next instruction is a new statement.

Two statements, two semicolons

System.out.println("Show: 6pm");
System.out.println("Seat A12");

4Comments do not run; names are for people

A // comments out the rest of that line. A /* ... */ block can span lines. Comments do not run.

Style is for humans: class names like TicketPrinter, names that say what they hold. The compiler accepts a and b; a reader cannot tell which is the seat.

Figure. A // comment does not run. The println of Seat A12 does. Names are for the person reading the file; the compiler accepts a and b, a reader cannot tell which is the seat.

How it works

  1. Comments do not run; names are for peopleComments do not run. Names are for the person reading the file.

Comment versus a statement that runs

// This line does not run.
System.out.println("Seat A12");
/* A block comment
   can cover several lines. */

5A backslash changes the next character

Inside a string, a backslash starts an escape. The backslash is not printed; it changes the next character.

\n is a new line, \t a tab, \" a quote that can sit inside quotes. A lone backslash at the end of a string is an error — it has nothing to change.

No diagram — the idea is carried by the prose or the Java listing.

Escapes you will type this week
EscapeMeaningOn screen
\nNew lineThe next text starts below
\tTabA wider gap
\"QuoteA " inside the string

One string, two lines of output

System.out.println("Seat A12\nPrice 200");

6Words before start, words after start

Command-line arguments arrive in main's args array — words typed after the program name, before it starts. args[0] is the first of those words, if any were given.

User input is different: the program is already running and waits. Scanner on System.in reads a line the person types. Do not treat args as a prompt; the person never saw a question.

Figure. args is ready at launch — words typed after the program name, before it starts. Scanner on System.in waits after launch. Do not treat args as a prompt; the person never saw a question.

How it works

  1. Words before start, words after startargs is ready at launch. Scanner waits for a person after launch.

args[0] then a typed seat

import java.util.Scanner;

public class TicketPrinter {
    public static void main(String[] args) {
        System.out.println(args[0]);
        Scanner in = new Scanner(System.in);
        String seat = in.nextLine();
        System.out.println(seat);
    }
}

Notes

  • In Object-Oriented Programming through Java because every later listing lives in a class with a main door — tokens, statements, comments, escapes, arguments and typed input are how that file is read.
  • The compiler splits the file into tokens. Most statements end with a semicolon.
  • Command-line arguments arrive before the program starts; Scanner on System.in waits for a person who is already watching.

Exam traps & shortcuts

  • A missing semicolon is not a style issue. The compiler cannot see where the instruction ended.
  • args is words typed after the program name. Scanner is words typed after it has started.

Recap

The class is the box, main is the door, tokens and semicolons are how the file is cut, and args is not a prompt.

A class is the box; main is the door
The machine starts at main. The class name and the file name should match.
The compiler reads tokens, not a story
A misspelled token is not almost right — it is a different token, or an error.
A statement is one instruction
Most statements end with a semicolon.
Words before start, words after start
args arrives at launch. Scanner waits for a person after launch.

Practise Java Program Structure

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.