- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
Floating-Point Variables
Floating-point numbers are the last category of primitive types that need to be covered. Floating-point 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, floating-point numbers are represented by the types float and double. Both of these follow a standard floating-point specification: IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Std. 754-1985 (IEEE, New York). The fact that these data types follow this specificationno matter what machine an application or applet is running onis one of the details that makes Java so portable. In other languages, floating-point operations are defined for the floating-point 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 floating-point 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 Floating-Point Types and Their Supported Ranges
Floating-Point Type |
Minimum Positive Value |
Default Value |
Maximum Value |
float |
1.40239846e45f |
0 |
3.40282347e+38f |
double |
4.94065645841246544e324d |
0 |
1.7976931348623157e+308d |
In addition to representing normal floating-point numbers, there are five unique states that can be assigned to float and double variables:
-
Negative infinity
-
Positive infinity
-
Positive zero
-
Negative zero
-
Not-a-Number (NaN)
These states are required, by definition of the IEEE 754-1985 standard, in part to account for rollover. For instance, adding one to the maximum number that can be stored in a floating-point 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, floating-point arithmetic never results in an exception. Dividing by zero produces a positive infinity or negative infinity result depending on the sign of the non-zero 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 floating-point 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 |