AP® Computer Science A · Unit 1 · Topic 1.1

Compilers, Bytecode, and Three Kinds of Error

Java source code must first be compiled into bytecode before it runs, and the compiler only catches errors in grammar — not errors in logic or errors that only occur while running.

Java is not run directly. The compiler reads your source code and checks that it obeys the rules of Java grammar: every statement ends in a semicolon, every variable is declared with a type, every type matches how it's used. If it passes, the compiler translates it into bytecode, a set of instructions the Java Virtual Machine can run on any computer. If it fails, you get a compile-time error and no bytecode is produced at all — the program never runs, not even once, not even the correct parts.

Once bytecode exists, the JVM executes it line by line. Some problems only appear here: dividing by zero, calling a method on a null reference, asking for index 10 of an array that only has 5 slots. These are run-time errors, and they crash the program partway through, after it has already started doing work. A third kind of problem is quieter: the program compiles, runs, finishes, and prints an answer — just the wrong one. That's a logic error, and neither the compiler nor the JVM will ever flag it, because nothing about it breaks Java's rules.

This progression matters because it tells you where to look when something goes wrong. A compile-time error means re-read your syntax before you even try running anything. A run-time error means the syntax was fine but something about the data or the logic caused a crash mid-execution. A logic error means you have to trace through the program's reasoning yourself, since the machine thinks everything is fine. Later topics on variables, expressions, and references all build on code that at least compiles — so this is the first filter every program has to pass through.

Key terms

compiler
A program that translates Java source code into bytecode and checks it for grammar errors first.
bytecode
The translated form of a Java program that the Java Virtual Machine actually executes.
compile-time error
A violation of Java's grammar rules caught during translation, before any bytecode is produced or run.
run-time error
A problem that only appears while the compiled program is executing, such as dividing by zero.
logic error
A mistake in a program's reasoning that lets it run to completion but produce the wrong result.

Watch it explained

  1. How it worksHow does the compiler turn Java source into bytecode that the virtual machine then runs?
  2. How it worksWalk me through where a compile-time error, a run-time error, and a logic error each show up.
  3. Worked exampleFind the compile-time error in int count = 3.7; and what exactly the compiler objects to.
  4. Common mistakeWhy isn't a program that compiles cleanly guaranteed to be correct?

Check yourself

Your turn1 of 2
A program computes average as total / count using ints, and total/count should be 82.5, but it prints 82. The program compiled with no errors and ran without crashing. What kind of problem is this?

End of topic.