 Data Types and Other Tokens
 Working with Variables
 The boolean Primitive
 The Flavors of Integer
 Operators
 Character Variables
 FloatingPoint Variables
 Literals: Assigning Values
 Integer Literals
 Character Literals
 FloatingPoint Literals
 String Literals
 Arrays
 NonToken Input Elements
 Troubleshooting
FloatingPoint Variables
Floatingpoint numbers are the last category of primitive types that need to be covered. Floatingpoint numbers are used to represent numbers that have a decimal point in them (such as 5.3 or 99.234). Whole numbers can also be represented, but as a floating point, the number 5 is actually 5.0.
In Java, floatingpoint numbers are represented by the types float and double. Both of these follow a standard floatingpoint specification: IEEE Standard for Binary FloatingPoint Arithmetic, ANSI/IEEE Std. 7541985 (IEEE, New York). The fact that these data types follow this specification—no matter what machine an application or applet is running on—is one of the details that makes Java so portable. In other languages, floatingpoint operations are defined for the floatingpoint unit (FPU) of the particular machine the program is executing on. For example, this means that the representation of 5.0 on an IBM PC might not be the same as on a DEC VAX.
Table 3.5 summarizes the numeric ranges supported for each of the floatingpoint types. It also defines the default value assigned to a float or double attribute of a class if it isn't explicitly initialized as part of its declaration.
Table 3.5 FloatingPoint Types and Their Supported Ranges
FloatingPoint Type 
Minimum Positive Value 
Default Value 
Maximum Value 
float 
1.40239846e–45f 
0 
3.40282347e+38f 
double 
4.94065645841246544e–324d 
0 
1.7976931348623157e+308d 
In addition to representing normal floatingpoint numbers, there are five unique states that can be assigned to float and double variables:

Negative infinity

Positive infinity

Positive zero

Negative zero

NotaNumber (NaN)
These states are required, by definition of the IEEE 7541985 standard, in part to account for rollover. For instance, adding one to the maximum number that can be stored in a floatingpoint type results in a positive infinity result. When an operation results in a number too small to be represented, this is an underflow condition that is represented by a zero state. Unlike some integer operations, floatingpoint arithmetic never results in an exception. Dividing by zero produces a positive infinity or negative infinity result depending on the sign of the nonzero operand (NaN results if both operands are zero). Positive or negative infinity also results if a positive operand is divided by a positive or negative zero, respectively. The Float wrapper class introduced in Chapter 7, "Classes," provides several useful methods for working with these special states.
Most of the operations that can be applied to integers have an analogous operation for floatingpoint numbers. The exceptions are the bitwise operations. The operators that can be used in expressions of type float or double are given in Table 3.6.
Table 3.6 Operations on float and double Expressions
Operation 
Description 
=, +=, –=, *=, /= 
Assignment operators 
==, != 
Equality and inequality operators 
<, <=, >, >= 
Relational operators 
+, – 
Unary sign operators 
+, –, *, /, % 
Addition, subtraction, multiplication, division, and remainder operators 
+=, –=, *=, /= 
Addition, subtraction, multiplication, division, and assign operators 
++, –– 
Increment and decrement operators 