Compile errors
The compiler is a strict proofreader. Before the score reaches the orchestra, it checks every line of it. When the proofreader finds a violation, it returns the text with notes in the margins, and the concert does not start.
Here is a program with one semicolon missing:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
System.out.println("Bye") // ✗
}
}error: ';' expectedThis is a compile error: the compiler refused to translate the text, and the program did not start. Hello is not printed either, even though the first instruction is correct. The proofreader returned the whole score, and the orchestra did not play a single bar.
Interviewers like to ask how a compile error differs from an error while the program runs. The compiler finds the first kind before launch, and not a single line runs. The second kind happens when the program is already running and some lines have already run.
Reading the message
In full, the first line of the message looks like this: Main.java:4: error: ';' expected. Let's read it from left to right:
Main.javais the file with the error.4is the line number.error: ';' expectedsays what is broken: a semicolon was expected here.
Under this line, javac repeats the program's line and puts a ^ sign where it noticed the error. At the end, it says how many errors it found: 1 error. When you run java Main.java, one more line is added, error: compilation failed.
An unknown name
The second common error is a name Java does not know. Here a letter is missing from the method name:
System.out.printn("Hi"); // ✗error: cannot find symbol
symbol: method printn(String)
location: variable out of type PrintStreamcannot find symbol means "the name was not found". The two lines under it say what was looked for and where:
symbolis the name that was not found: the methodprintn, given a string;locationis where it was looked for: inSystem.out;PrintStreamis the class that holdsprintandprintln.
Fix the name to println, and the program compiles.
This is where the proofreader picture falls short. A real proofreader would also catch a typo in the word "Helo". The compiler checks the language's rules and names, but it does not read text in quotes: it lets println("Helo") through, and you see the typo only in the output.
First error first
One error can produce several messages. Here the quotes around the text are missing:
System.out.println(Hello Java); // ✗error: ')' or ',' expected
error: not a statement
error: ';' expectedWithout quotes, the compiler reads Hello and Java as two names in a row and gets confused: three messages for one line. Add the quotes, "Hello Java", and all three go away.
So fix errors from the top down. After fixing the first one, compile again: the messages after it often disappear along with it.
Common mistake
A name was written with a small letter:
system.out.println("Hi"); // ✗error: package system does not existJava tells letter case apart: capital and small letters. For Java, System and system are two different names, and the second one does not exist. The message looks odd: javac decided that system is a package, a group of classes, and found no such package. But the cause is one small s.
In the same way, String differs from string and main from Main. Names in Java are written exactly as they were defined, letter for letter.
Key points
- The compiler finds a compile error before launch: the program does not start, and not a single line runs, not even a correct one.
- The message
Main.java:4: error: …names the file, the line number and the broken rule. cannot find symbolmeans Java does not know the name; Java tells capital and small letters apart in every name.- Fix the first error first: the other messages often disappear along with it.