Java Reference
In-Depth Information
The boolean Data Type
The boolean data type has only two valid values: true and false . These two values are called boolean literals. You can
use boolean literals as
boolean done; // Declares a boolean variable named done
done = true; // Assigns true to done
One important point to note is that a boolean variable cannot be cast to any other data type and vice versa. Java
does not specify the size of the boolean data type. Its size is left up to the JVM implementation. Typically, a value of a
boolean data type is stored internally in a byte.
Floating-Point Data Types
A number that contains a fractional part is known as a real number, for example, 3.25, 0.49, -9.19, etc. A computer
stores every number, real or integral, in binary format, which consists of only 0s and 1s. Therefore, it is necessary to
convert a real number to its binary representation before it can be stored. It must be converted back to a real number
after its binary representation is read. When a real number is converted to its binary representation, the computer
must also store the position of the decimal point within the number. There are two strategies to store a real number in
computer memory.
Store only the binary representation of the number and assume that there are always a fixed
number of digits before and after the point. A point is called a decimal point in the decimal
representation of a number and a binary point in the binary representation. The type of
representation in which the position of the point is always fixed in a number is known as
fixed-point number format.
Store the binary representation of the real number and the position of the point in the
real number. Since the number of digits before and after the point can vary in this kind of
representation of the real number, we say that the point can float. This kind of representation
is called a floating-point format.
Floating-point representations are slower and less accurate compared to fixed-point representations. However,
floating-point representations can handle a larger range of numbers with the same computer memory as compared to
that of fixed-point representations.
Java supports the floating-point number format. It is important to note that not all real numbers have exact
binary representations, and therefore they are represented as floating-point approximations. Java uses the IEEE 754
Floating-Point standard to store real numbers. IEEE is acronym for the Institute of Electrical and Electronic Engineers.
Java has two floating-point Numeric data types:
float
double
The float Data Type
The float data type uses 32 bits to store a floating-point number in the IEEE 754 standard format. A floating-point
number represented in 32 bits according to the IEEE 754 standard is also known as a single-precision floating-point
number. It can represent a real number as small as 1.4 x 10 -45 and as big as 3.4 x 10 38 (approx.) in magnitude. The
range includes only the magnitude. It could be positive or negative. Here, 1.4 x 10 -45 is the smallest positive number
greater than zero that can be stored in a float variable.
 
Search WWH ::




Custom Search