Java Reference
In-Depth Information
}
}
Recompiling this code will then reveal the uppercase mistyping of variable
Val
. Once corrected, the program can finally be compiled into an executable
bytecode. This toy example emphasizes the incremental strategy for correcting
programs.
In general, correcting a program consists of first making it syntactically correct
so that the compiler can indeed parse the code and generate a corresponding
bytecode. Then comes the task of making the program
semantically correct
.
That is, to test and better prove that the program indeed solves the problem in
all
instances and not only on a particular test suite. This is a far more dicult
and challenging task that has not yet been solved today. For very specific
codes like embarked codes on airplanes, there exist mathematical techniques
12
to prove that these codes are indeed correct and resilient to all kinds of input.
But these techniques hold only for these very specific codes. So be prepared
in your programming life to discover bugs later on, even if you have already
successfully used some code for many years. Remember that unless you prove
your code, you cannot guarantee that it is crash-free.
Proving that a program is correct is all the more dicult since numbers are
represented in machines using
finite representations
(4 or 8 bytes depending
on whether single or double precision is used). Thus usually remarkable
mathematical identities like
e
log
x
= log
e
x
=
x
on variables may fail as they
do not detect potential
overflow
problems that yield not only incorrect result
but may potentially crash a system. For example, consider the two primitives
types for storing natural numbers in Java:
-
int
:the
maximum
machine-representable
int
is 2
31
−
1.
-
long
:the
maximum
machine-representable
long
is 2
63
−
1.
Let us now perform some incrementation instructions as follows:
class
OverflowBug
{
public static void
main( String
[ ]
args )
int
n=2147483647;
//2^31-1
int
nval=n+1;
System . out . println (
"If n="
+n+
" then n+1="
+nval ) ;
long
m=9223372036854775807L;
// 2^63-1
long
mval=m+1;
System . out . println (
"If m="
+m+
" then m+1="
+mval ) ;
12
For example, the branch of so-called
static analysis
.
Search WWH ::
Custom Search