AP Computer Science A · Study guide

Compilation and Execution

Unit 1 · Using Objects and Methods · Topic 1.1

Compilation and Execution

What happens between typing a program and running it — and why "it compiles" is a much weaker claim than it sounds.

Exam-day recap
  • There are two gates: compile asks "Is this legal Java?" before the program starts; run asks "Does it work?" while executing.
  • Code can be written in any text editor; an IDE is common because it bundles tools to write, compile, and run.
  • "An IDE is required to write Java" is false, and so is "an IDE makes the program run faster."
  • A compiler checks code for some errors; errors it can detect must be fixed before the program can be run.
  • A method that multiplies instead of divides compiles perfectly — intent is invisible to a compiler, which checks the language's rules (syntax, types, declarations), never meaning.
  • The compiler catches rule violations like int total = "twelve";, a missing semicolon, or an undeclared name.
  • A compile error means there is no program — nothing executes, not the valid lines above the error, not main.
  • For "What is the output?" with a buried compile error, there is no output at all — scan for compile errors first.
  • "It compiled" means the code follows the language's rules and nothing more — it says nothing about whether the program is right.
  • A program can compile and still crash because a zero divisor or out-of-range index depends on run-time values.

1Two Gates, Not One

Between the code you type and a program that does something, there are two separate gates. Most confusion in this topic comes from treating them as one.

Gate 1 — CompileGate 2 — Run
WhenBefore the program startsWhile the program is executing
Asks"Is this legal Java?""Does this work?"
Fails withA refusal to produce a program at allA crash, or a wrong answer
You seeCompiler messages, no executionOutput — possibly the wrong output

Drawn end to end, the two gates sit in different places and refuse in different ways:

The learning objective

1.1.B — explain the code compilation and execution process. Two essential knowledge statements: where code is written, and what the compiler does about errors. The second is where the exam questions live.

2Where Code Is Written

Essential knowledge 1.1.B.1

Code can be written in any text editor; however, an integrated development environment (IDE) is often used because it provides tools for a programmer to write, compile, and run code.

Both halves matter. Java source is ordinary text — a .java file opened in the simplest editor on the machine is a completely valid way to write a program. Nothing about the language requires special software.

An IDE is chosen for convenience, not necessity. The course names the three things it bundles: write, compile, run. That is the whole justification — one place to do all three, instead of an editor plus separate commands.

A distractor to recognize

"An IDE is required to write Java" is false, and it appears as a wrong option. So does "an IDE makes the program run faster" — the IDE is a workspace, not part of the finished program. Nothing an IDE does changes what the compiled program is.

3What the Compiler Actually Does

Essential knowledge 1.1.B.2

A compiler checks code for some errors. Errors detectable by the compiler need to be fixed before the program can be run.

Two clauses, and the first word of the first clause carries the topic.

3.1 "Some" Is the Whole Lesson

The essential knowledge does not say the compiler checks for errors. It says it checks for some errors. Everything the compiler cannot see is still in the program when it runs.

public static double goalsPerGame(int goals, int games)
{
   return goals * games;
}

This compiles perfectly. Every name is declared, every type lines up, every brace is matched. It is also obviously wrong — it should divide, not multiply. Ask it about Haaland's 27 goals in 31 games and it reports 837 goals per game without hesitation.

Why the compiler cannot help here

The compiler checks that the code follows the rules of the language. goals * games is a legal multiplication of two int values producing a number the method is allowed to return. Nothing in the language says a method named goalsPerGame must divide. Intent is invisible to a compiler — it can verify the language's rules, never meaning.

3.2 What It Does Catch

Rule violations, and they stop the program from being produced at all:

int total = "twelve";        // a String is not an int
System.out.println(score)     // missing semicolon
System.out.println(missing);  // never declared

Put the two classes of error side by side and the word some stops being a hedge:

Each of these breaks a rule the compiler is able to check by reading the code, so each is reported before anything runs.

3.3 "Before the Program Can Be Run" Is Literal

The second clause is stricter than students expect. A compile error does not mean the program runs badly — it means there is no program. Nothing executes. Not the correct lines above the error, not main, nothing.

public static void main(String[] args)
{
   System.out.println("Starting up");   // perfectly fine
   int count = "three";                    // compile error
   System.out.println(count);
}

Starting up is never printed. The first line is valid, but validity is judged for the whole file — and a file that fails to compile produces nothing to execute.

The question this is asked as

"What is the output of the following program?" with a compile error buried in it. The answer is not the output of the lines above the error. It is that the program does not compile, so there is no output at all. Scan for compile errors before tracing anything.

4The Pipeline End to End

StageWhat existsWhat can go wrong
WriteA .java source file — ordinary textNothing is checked yet; anything at all can be typed
CompileThe compiler reads the sourceRule violations are reported; nothing is produced
RunThe compiled program executesIt crashes, or produces a wrong answer
The mental model worth carrying

The compiler is a proofreader for the language's rules (syntax, types, declarations), not an editor for sense. It will stop a sentence that is not English. It will not stop a sentence that is fluent and false. "It compiled" means only that those rules are met — it says nothing whatever about whether the program is right.

4.1 A Program That Passes Both Gates and Is Still Wrong

Player son = new Player("Son", 14, 30);
Player salah = new Player("Salah", 19, 33);

System.out.println(goalsPerGame(14, 30));   // 420.0
System.out.println(goalsPerGame(19, 33));   // 627.0

Compiles, runs, prints two numbers, exits cleanly. No error message appears anywhere at any stage. The only thing that reveals the bug is a person who knows a striker does not score 420 goals per game — which is testing, not compiling.

5Check Yourself

A file has one missing semicolon on line 40 of 60. What runs?

Response

Nothing. A missing semicolon breaks a rule of the language, so the compiler reports it and produces no program. Errors detectable by the compiler must be fixed before the program can be run — lines 1 to 39 do not execute "up to" the error.

Is an IDE required to compile a Java program?

Response

No. Code can be written in any text editor. An IDE is commonly used because it bundles tools to write, compile, and run code in one place, but nothing about the language depends on it.

A program prints an average of 837 goals per game. Did the compiler fail?

Response

No — it did its job. The compiler checks code against the rules of the language, and multiplying two int values is entirely legal. The mistake is in the algorithm, which is exactly the class of error a compiler cannot detect. That is why the essential knowledge says some errors.

Why can a program compile and still crash?

Response

Because compiling only establishes that the code is legal. Whether a division ends up with a zero on the right, or an index ends up past the end of an array, depends on values that do not exist until the program runs. The compiler cannot see them, so those failures wait for execution.

6Summary

IdeaWhat to remember
Where code is writtenAny text editor works; an IDE is common but not required
Why an IDEIt bundles tools to write, compile, and run
What the compiler checksSome errors — violations of the language's rules
What it cannot checkWhether the program does the right thing
Compile errorMust be fixed before the program can run — nothing executes
"It compiled"Means legal, not correct
Exam reflexCheck for compile errors before tracing output
The habit

Treat compiling as the lowest bar a program clears, not the finish line. A clean compile means the language's rules are met and nothing more. Everything you actually care about — whether the answer is right — is established after that, by running the program on data whose answer you already know.