Java Reference
In-Depth Information
is impossible for us to consider all numerical imprecisions that can occur. 6
Consider the following example:
Program 2.12 A simple numerical bug
// Constant
final double PI = 3.14;
int a=1;
double b=a+P I ;
if (b==4.14) // Equality test are dangerous!!!
System . out . println ( "Correct result" );
else
{ System . out . println ( "Incorrect result" );
System . out . println ( "a=" +a+ "b=" +b+ "PI=" +PI ) ;
}
This code is dangerous because, mathematically speaking, it is obvious that
a + b =4 . 14 but because of the finite representation of numbers in machine (and
their various formatting), this simple addition yields an approximate result. In
practice, the first lesson we learn is that we always need to very cautiously use
equality tests on reals. The second lesson is that proofs of programs should be
fully automated. This is a very active domain of theoretical computer science
that will bring novel solutions in the 21st century.
2.7 Parsing program arguments from the
command line
So far we have initialized programs either by interactively asking users to enter
initial values at the console, or by plugging these initial values directly into the
source code. The former approach means that we have high-latency programs
since user input is “slow.” The latter means that programs lack flexibility since
we need to recompile the code every time we would like to test other initial
parameter conditions.
Fortunately, programs in Java can be executed with arguments given in the
command line. These arguments are stored in the array arg of the main
function:
public static void main (String[] args)
These arguments are stored as strings args[0] , args[1] ,etc.Thusevenifwe
enter numbers like ”120” and ”28” in the command line:
6 Some software packages such as Astree used in the airplane industry do that
automatically to certify code robustness. See http://www.astree.ens.fr/
 
 
Search WWH ::




Custom Search