2.3. Floating-Point Data Types
Numeric values that are not integral are stored as floating-point numbers. A floating-point number has a fixed number of digits of accuracy but with a very wide range of values. You get a wide range of values, even though the number of digits is fixed, because the decimal point can "float." For example, the values 0.000005, 500.0, and 5000000000000.0 can be written as 5×10−6, 5×102, and 5×1012 respectively—you have just one digit 5 but you get three different numbers by moving the decimal point around.
There are two primitive floating-point types in Java, type float and type double. These give you a choice in the number of digits precision available to represent your data values, and in the range of values that can be accommodated:
Data Type | Description |
---|---|
float | Variables of this type can have values from −3.4E38 (-3.4 * 1038) to +3.4E38 (+3.4 * 1038) and occupy 4 bytes in memory. Values are represented with approximately 7 decimal digits accuracy. |
double | Variables of this type can have values from −1.7E308 (-1.7 * 10308) to +1.7E308 (+1.7 * 10308) and occupy 8 bytes in memory. Values are represented with approximately 17 decimal digits accuracy. The smallest non-zero value that you can have is roughly (4.9 * 10−324. |
NOTE
All floating-point operations and the definitions for values of type float and type double conform to the IEEE 754 standard.
As with integer calculations, floating-point calculations in Java will produce the same results on any computer. ...
Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.