What Is a Syntax Error in Java?

By Kevin Walker

Java syntax errors refer to mistakes made by a programmer in implementing the grammar of the Java programming language. It doesn't cover mistakes in logic of the program itself.

Java Syntax

Java, like all other programming languages, has its own syntax. For example, one rule of Java syntax is that all commands must end with a semicolon (;). Java syntax is vastly simpler than the syntax of English or any other "natural" language but it is also much stricter. Leaving a comma out of sentence in English merely makes the writer look sloppy. A trivial mistake in Java syntax make the commands unintelligible to the computer.

Compiler Error

Syntax errors are a type of compiler error. This means they will be detected immediately when the programmer tries to convert his source code into a program. This is opposed to runtime errors, which are not detected until the program is actually running.

Examples of Syntax Errors

This code -- if x=3 System.out.println("Hello.") -- has three syntax errors:First, the command does not end in a semicolon. Second, the comparison that occurs after the "if" must be surrounded by parenthesis. Finally, the comparison itself uses the assignment operator "=" rather than the comparison operator "==." Any one of these will generate a Java syntax error. Here is the command written properly:if (x==3) System.out.println("Hello.");

References & Resources