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 punctuation is legal in the language. The
syntax is often called the language's grammar rules . For Java, the syntax describes what
arrangements of words and punctuation are allowed in a class or program 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
program 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, which 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—it 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. 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 output
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 message.
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 the output is incorrect. For
example, if you were to mistakenly use the multiplication sign in place of the addition
sign, it 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