THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
long 64-bit integer (signed)

float 32-bit floating-point (IEEE 754)

double 64-bit floating-point (IEEE 754)

For each primitive type there is also a corresponding object type, generally termed a "wrapper" class. For
example, the class Integer is the wrapper class for int. In most contexts, the language automatically
converts between primitive types and objects of the wrapper class if one type is used where the other is
expected.


In the Fibonacci program, we declared hi and lo with initial values of 1. The initial values are set by
initialization expressions, using the = operator, when the variables are declared. The = operator (also called
the assignment operator), sets the variable named on the left-hand side to the value of the expression on the
right-hand side.


Local variables are undefined prior to initialization. You don't have to initialize them at the point at which you
declare them, but if you try to use local variables before assigning a value, the compiler will refuse to compile
your program until you fix the problem.


As both lo and hi are of the same type, we could have used a short-hand form for declaring them. We can
declare more than one variable of a given type by separating the variable names (with their initialization
expressions) by commas. We could replace the first two lines of main with the single equivalent line


int lo = 1, hi = 1;


or the much more readable


int lo = 1,
hi = 1;


Notice that the presence of line breaks makes no difference to the meaning of the statementline breaks, spaces,
tabs, and other whitespace are purely for the programmer's convenience.


The while statement in the example demonstrates one way of looping. The expression inside the while is
evaluatedif the expression is true, the loop's body is executed and the expression is tested again. The while
is repeated until the expression becomes false. If it never becomes false, the loop will run forever unless
something intervenes to break out of the loop, such as a break statement or an exception.


The body of the while consists of a single statement. That could be a simple statement (such as a method
invocation), another control-flow statement, or a blockzero or more individual statements enclosed in curly
braces.


The expression that while tests is a boolean expression that has the value true or false. Boolean
expressions can be formed with the comparison operators (<, <=, >, >=) to compare the relative magnitudes
of two values or with the == operator or != operator to test for equality or inequality, respectively. The
boolean expression hi< 50 in the example tests whether the current high value of the sequence is less than



  1. If the high value is less than 50, its value is printed and the next value is calculated. If the high value
    equals or exceeds 50, control passes to the first line of code following the body of the while loop. That is the
    end of the main method in this example, so the program is finished.

Free download pdf