What Is a Syntax Error in Java?

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

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.

Advertisement

Java Syntax

Video of the Day

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.

Advertisement

Video of the Day

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.

Advertisement

Examples of Syntax Errors

This code -- if x=3 System.out.println("Hello.") -- has three syntax errors:

Advertisement

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.");

Advertisement

Advertisement

references