Java Reference
In-Depth Information
Syntax and Semantics
The description of a programming language, or any other kind of language, can be thought
of as having two parts, called the syntax and semantics of the language.
The syntax tells what arrangement of words and punctuations are legal in the language. The
syntax of the language is often called the grammar rules of the language. For Java, the syn-
tax describes what arrangements of words and punctuations are allowed in a class or pro-
gram definition.
The semantics of a language describes the meaning of things written while following the
syntax rules of the language. For a Java program, the syntax describes how you write a pro-
gram and the semantics describes what happens when you run the program.
When writing a program in Java, you are always using both the syntax and the semantics of
the Java language.
TIP: Error Messages
A mistake in a program is called a bug . For this reason, the process of eliminating
mistakes in your program is called debugging . There are three commonly recognized
types of bugs or errors, and they are known as syntax errors , run-time errors , and logic
errors . Let's consider them in order.
A syntax error is a grammatical mistake in your program; that is, a mistake in the
allowed arrangement of words and punctuations. If you violate one of these rules, for
example, by omitting a required punctuation, that is a syntax error. The compiler
will catch syntax errors and output an error message telling you that it has found the
error, where it thinks the error is, and what it thinks the error is. If the compiler says
you have a syntax error, you undoubtedly do have an error. However, the compiler
could be incorrect about where and what the error is.
An error that is not detected until your program is run is called a run-time error .
If the computer detects a run-time error when your program is run, then it will out-
put an error message. The error message may not be easy to understand, but at least
it lets you know that something is wrong.
A mistake in the underlying algorithm for your program is called a logic error . If
your program has only logic errors, it will compile and run without any error mes-
sage. You have written a valid Java program, but you have not written a program that
does what you want. The program runs and gives output, but gives incorrect output.
For example, if you were to mistakenly use the multiplication sign in place of the
addition sign, that would be a logic error. Logic errors are the hardest kind of error to
locate, because the computer does not give you any error messages.
bug
debugging
syntax error
run-time
error
logic error
Search WWH ::




Custom Search