Compile-Time Errors

In this lesson, we will explore some common syntax errors in Java and the messages they cause a compiler to generate.

Common mistakes

We begin this lesson by listing the following mistakes in Java’s syntax, because they are easy to make, particularly for someone just learning Java:

  • Missing semicolons. Each Java statement must end in a semicolon, just as English statements end in a period.
  • Extra semicolons. An extra semicolon sometimes causes no harm, as the compiler simply ignores it. However, a misplaced semicolon can cause a problem.
  • Capitalization. Java is case-sensitive, meaning our use of uppercase and lowercase letters matters. All Java reserved words are lowercase. Spelling one using one or more capital letters is incorrect. Any identifiers that we use must be consistent in their case.
  • Spelling. Errors while typing Java reserved words or variable names, for example, are typically flagged as unidentified symbols.
  • Mismatched parentheses or braces. Each open parenthesis or brace must match a corresponding close parenthesis or brace, respectively. Usually, an IDE’s editor will help us to avoid this error, but if we make it anyway, the compiler will complain.
  • Missing quotes. String literals must begin and end with a double quote. Failing to end a string with a double quote in a println statement is a common mistake.
  • Missing operators. A concatenation operator in a println statement or a multiplication operator in a simple expression like 2y are details that are easy to overlook.
  • Uninitialized variables. Failing to give a variable a value after its declaration and before its use is not always a mistake. The compiler, however, likely will warn us about this apparent omission. This warning will prevent program execution, even if our logic is correct.

The examples that follow in this lesson and the next one examine each of these syntax errors.

Missing semicolons

Let’s consider the program given below. Click the Run button and look at the messages given during its compilation.

Get hands-on with 1200+ tech skills courses.