Before answering the questions in this chapter you should be able to: name the main hardware
components of a computer and say what each one does; distinguish system software from
application software; explain the difference between source code, bytecode and machine code;
describe what javac and java each do; explain why Java is portable; write, from
memory, the smallest complete Java program that prints a line of text; and classify an error as
compile-time, run-time or logic.
Hardware
A computer is organised around four kinds of component.
- CPU (processor). Executes instructions. Its control unit fetches and decodes instructions; its ALU performs arithmetic and logic. Speed is measured in hertz (GHz = billions of cycles per second).
- Main memory (RAM). Holds the program and its data while the program runs. It is volatile: switch the power off and the contents are lost. Access is fast and random (any address costs the same).
- Secondary storage. Hard disk, SSD, USB drive, optical disc. Non-volatile,
larger, much slower than RAM. This is where your
.javaand.classfiles live. - Input/output devices. Keyboard, mouse, touchscreen (input); monitor, printer, speakers (output). A touchscreen and a hard disk are both input and output.
Storage units: 1 byte = 8 bits; 1 KB ≈ 10³ bytes; 1 MB ≈ 10⁶; 1 GB ≈ 10⁹; 1 TB ≈ 10¹².
Software
System software manages the machine: the operating system, device drivers, utilities, and the language tools (compiler, JVM). Application software does a job for the user: a browser, a spreadsheet, a game, the program you are about to write.
Languages and translation
- Machine language — binary instructions the CPU executes directly. Different for each processor family.
- Assembly language — mnemonics for machine instructions; needs an assembler.
- High-level language — Java, C, Python: readable, processor-independent, needs a compiler or an interpreter.
A compiler translates the whole program before it runs and reports all syntax errors at once. An interpreter translates and executes one statement at a time.
How Java is built and run
| Artefact | Produced by | What it is |
|---|---|---|
Hello.java |
you, in an editor/IDE | source code: text a human can read |
Hello.class |
javac Hello.java |
bytecode (object code): instructions for the JVM, not for any real CPU |
| output | java Hello |
the JVM loads the bytecode and executes it on this machine |
The JVM (Java Virtual Machine) is the program that executes compiled Java code on a
specific platform. Because every platform has its own JVM but they all accept the same bytecode,
the same .class file runs on Windows, macOS and Linux without recompilation. This is
portability, often quoted as write once, run anywhere. Java is therefore
both compiled and interpreted: compiled to bytecode, then interpreted (and partly
JIT-compiled) by the JVM.
The JDK (Java Development Kit) contains the compiler and development tools; it includes the JRE (Java Runtime Environment), which contains the JVM and the standard class library. You need the JDK to write Java; a user only needs a JRE to run it.
The smallest complete program
public class Hello { // class heading; file must be Hello.java
public static void main(String[] args) { // entry point of the application
System.out.println("Hello, world!"); // statement; ends with a semicolon
}
}
Things to notice, each of which has been an exam question:
- The keyword
classis required to declare a class.publicis an access modifier, not the declaration keyword. - A
publicclass must be stored in a file with exactly the same name plus.java. - The entry point must be
public static void main(String[] args). The parameter name is free (args,tracks,moat…), the type is not.String... args(varargs) is also accepted;String argsalone is not. - Java is case sensitive:
Main,mainandMAINare three different identifiers. - Braces
{ }delimit a block; each statement ends with a semicolon. System.out.println(x)printsxand moves to a new line;System.out.print(x)printsxand stays on the same line.- Comments:
// to end of line,/* ... */over several lines, and/** ... */for javadoc. Comments do not affect execution — they affect the human who reads your code, including the grader.
Identifiers
An identifier may contain letters, digits, the underscore and the dollar sign; it may
not begin with a digit; it may not be a reserved word (class, int,
public, new, …); and it may not contain a space or a hyphen. Convention:
ClassNamesLikeThis, methodAndVariableNamesLikeThis, CONSTANTS_LIKE_THIS.
Three kinds of error
- Compile-time (syntax) error — the compiler refuses to produce a
.classfile: missing semicolon, misspelled keyword, undeclared variable, wrong type. - Run-time error — the program compiles but fails while running: division by
zero, using a
nullreference, indexing past the end of an array. - Logic error — the program compiles and runs, but produces the wrong result:
the wrong formula,
<where<=was meant. The compiler cannot help you; only testing and tracing can.
- Source code → (
javac) → bytecode → (java, i.e. the JVM) → output. - RAM is volatile and holds the running program; disk is non-volatile and holds the files.
- The JVM is what makes bytecode portable.
public static void main(String[] args)— learn it letter by letter.- The compiler catches syntax errors, never logic errors.
- Running
java Hello.classinstead ofjava Hello. The launcher takes the class name, without the extension. - Writing
Public classorVoid main: Java keywords are all lower-case. - Assuming a program that compiles is a program that is correct.
Ready? Close the notes and practise.
40 questions. Predict the output before you check — that is the skill the exam measures.