mainmethod does is declare and initialize some variables. Here is the relevant section of code:
double tolerance = .000000001; // Our approximation of zero int max_count = 200; // Maximum number of Newton's method iterations /* x is our current guess. If no command line guess is given, we take 0 as our starting point. */ double x = 0;Java provides an assortment of primitive data types to represent numbers and characters. Here we see two of these types. The first type, which we have seen before in method declarations, is the floating point
double tolerance = .000000001;This line declares the existence of the variable
toleranceand initializes its initial value to .000000001. This is equivalent to the two lines:
double tolerance; tolerance = .000000001;A variable declaration can occur anywhere in a method and can be used anywhere "within its scope". By this I mean anywhere after the declaration and within the set of curly braces that enclose it. So, if you declare some variables at the beginning of the method, like we have done in the
mainmethod, you can use those variables everywhere in the method.
We see in our method's variable declarations another data type - the
integer. The variable
max_count is of type
a 32 bit signed integer, so
it can take on integer values roughly between plus and minus one billion.
We have seen, then, that the
main method declares three variables
doublewhich stores how close to zero we have to come before we declare a root has been found.
intwhich stores the maximum number of iterations of Newton's method that we will try before we give up on finding a root.
doublewhich stores our current guess of the location of the root.
Since we are talking about primitive variables, I will take this opportunity to mention a couple of other data types that you might find in Java code.
There is a special data type for truth in Java, the
A variable of type
boolean can take on only two values,
Another data type that you might see is
char. This represents
a single character, so you could make a declaration like this:
char the_letter_a = 'a';Notice that a character in back-quotes like
'a'is taken to be of type
|5.7.2 Command Line Arguments and Arrays|