AP Computer Science A · Unit 1 lecture · Part 1 of 11 · 8 min

1.1 Algorithms, programming and compilersFrom an idea to a running program

By the end: Trace any Unit 1 expression, method call or String operation exactly the way Java runs it, and write short methods that call the methods you are given.
Mr. Thompkins
Welcome to AP CSA. I'm Mr. T, and this whole course runs on one skill: reading code the way the computer reads it, one line at a time, with no guessing. Today we start at the very beginning: what a program is, and what happens when it breaks.

An algorithm is a step-by-step process that solves a problem or finishes a task. You already follow algorithms all day. A recipe is one. So is "to get to school: walk two blocks, turn left at the bakery, cross at the light." The steps happen in a fixed order, and that order matters. Doing the steps one after another, top to bottom, is called sequencing, and it's the first of three building blocks every algorithm is made of. The other two, selection (choosing) and iteration (repeating), are Unit 2.

A program is an algorithm written in a programming language, here Java. A computer is fast but completely literal: it does exactly what you wrote, never what you meant.

What happens when you press Run

You write Java as source code, in a file whose name ends in .java. Before the program can run, a program called the compiler reads all of your source code and checks it against Java's rules. If it finds a problem, it stops and shows you an error message. If everything follows the rules, it translates your code into bytecode, a compact form the Java Virtual Machine (the JVM) can run. The JVM is what actually runs your program and produces the output. An IDE (integrated development environment) is the app that bundles all of this: an editor, the compiler and a Run button.

Pay.javasource codecompilerbytecodeJVM runs itoutputsyntax errornothing runsrun-time errorcrashes midwaylogic errorwrong answeronly testing catches a logic error
From source code to output. Each kind of error shows up at a different point. A logic error is the dangerous one: the program finishes and gives you a wrong answer without any warning.
?Wait, what's a class? We haven't learned that yet.
For now, think of a class as the container that Java code lives in. Every Java program has at least one, and its name matches the file name (class Greeting lives in Greeting.java). Later in this unit you'll use classes other people wrote, and in Unit 3 you'll write your own.

Here's a complete Java program. Read it line by line. The braces { } mark where the class and the main method begin and end, and each statement ends with a semicolon.

1public class Greeting12{3    public static void main(String[] args)24    {5        System.out.print("Hi, ");36        System.out.println("Mr. T");47        System.out.println("Line two");48    }9}
  1. line 1 Every Java program lives inside a class
  2. line 3 The program starts running here, at main
  3. line 5 print stays on the same line
  4. line 6-7 println prints, then moves to a new line
output › Hi, Mr. T Line two
A complete program, run under Java 17. The output box shows exactly what it prints.

Three kinds of errors

A syntax error (or compile-time error) breaks the rules of the language, like a missing semicolon or a misspelled keyword. The compiler catches it, so nothing runs. A run-time error happens while the program runs: the code is legal, but something impossible happens, like dividing a whole number by zero. Java stops and reports an exception, here an ArithmeticException.

A logic error is the sneaky one. The program runs to the end, but the answer is wrong, and nothing warns you. The only way to find it is testing: running the program on inputs whose answers you already know, including unusual ones like 0, negative numbers and empty text.

ErrorWhen it shows upExampleWhat you see
Syntax (compile-time)Before the program runsint x = 4.5; or a missing ;An error message, no program
Run-time (exception)While the program runsint share = 12 / people; with people 0ArithmeticException, program stops
LogicNever announcedarea = 1 / 2 * b * h; gives 0It runs and prints a wrong answer
The three kinds of errors. The exam asks you to tell them apart.
?Wait, what's an exception again?
An exception is an error that happens while the program is running, not while it's being compiled. When one happens and nothing handles it, the program stops and Java prints the exception's name, such as ArithmeticException. You'll meet two more in this unit: NullPointerException and StringIndexOutOfBoundsException.
Pause and think
A student's program averages two test scores. It compiles and runs, but for scores of 88 and 91 it prints 89 instead of 89.5. What kind of error is this, and how would the student have found it?
I thought about it. Show me
A logic error. The program follows every rule and runs to the end, so neither the compiler nor the JVM complains. The student finds it only by testing with scores whose average they already know, and noticing that the answer is wrong. (You'll see the exact cause in topic 1.3: dividing two whole numbers throws away the decimal part.)
Classify the error

Each segment below is meant to compute something about splitting 12 slices of pizza. For each one, say whether it has a syntax error, a run-time error, a logic error, or no error.

Segment I
int slices = 12
int people = 4;

Segment II
int slices = 12;
int people = 0;
int share = slices / people;

Segment III
double base = 6;
double height = 4;
double area = 1 / 2 * base * height;
  1. Segment I: the first line has no semicolon at the end. Java requires one after every statement, so the compiler stops. That's a syntax error.
    Why?
    The compiler reads the whole file first. Even one missing semicolon means nothing runs.
  2. Segment II: every line follows the rules, so it compiles. But when the third line runs, people is 0, and dividing an int by 0 is impossible. Java throws an ArithmeticException. That's a run-time error.
    Why?
    The compiler doesn't know what value people will hold when the line runs. It only checks the rules, so the problem shows up while the program runs.
  3. Segment III: it compiles and runs. But 1 / 2 divides two whole numbers, which gives 0 (you'll see why in 1.3), so the area is 0.0 instead of 12.0. No message appears. That's a logic error.
    Why?
    The rubric idea: a logic error is any program that runs to the end and gives an answer that doesn't match what it was supposed to compute.

Answer: I: syntax error. II: run-time error (ArithmeticException). III: logic error (area is 0.0, not 12.0).

Stuck? Get a hint
  1. Ask the questions in order: does it follow Java's rules? If not, it's a syntax error.
  2. If it compiles, does something impossible happen while it runs, like dividing by zero?
  3. If it runs to the end, is the answer what the programmer wanted? If not, it's a logic error.
Check yourself
A program compiles with no errors. Which statement must be true?
  1. AIt will produce the correct output for every input.
  2. BIt follows Java's syntax rules, but it may still crash or give wrong answers.
  3. CIt cannot throw an exception while it runs.
  4. DIt has been tested on all possible inputs.
Show the answer

B. Compiling checks only the rules of the language. The program can still throw an exception at run time (choice C is the trap) or contain a logic error, so choices A, C and D all claim more than compiling can promise.

  • An algorithm is a step-by-step process. Sequencing means the steps run in order, top to bottom.
  • The compiler checks your source code and translates it to bytecode. The JVM runs the bytecode.
  • Syntax error: caught by the compiler, nothing runs. Run-time error: an exception stops the program. Logic error: it runs and gives a wrong answer.
  • Test with inputs whose answers you know. That's the only way to catch logic errors.