Syntax Errors

When we are writing Java programs, the compiler is our first line of defense against errors. It can catch syntax errors.

Syntax errors represent grammar errors in the use of the programming language. They are the easiest to find and correct. The compiler will tell you where it got into trouble, and its best guess as to what you did wrong.

Some common syntax errors are:

  • Misspelled variable and method names
  • Omitting semicolons ;
  • Omitting closing parenthesis ), square bracket ], or curly brace }

Here’s an example of a syntax error message:

Debug.java:5: error: ';' expected

Usually the error is on the exact line indicated by the compiler, or the line just before it; however, if the problem is incorrectly nested braces, the actual error may be at the beginning of the nested block.



Try compiling the code and find the syntax error in Debug.java.

Folder Icon

Take this course for free

Already have an account?