A floating-point number is one which may have a fractional part. A floating-point constant can be written in one of two ways:
• the normal way, with an optional sign, and including a decimal point; for example, -3.75, 0.537, 81.792, 47.0;
• using scientific notation, with an optional sign, including a decimal point and including an ‘exponent’ part; for example, -0.375E1 meaning “-0.375 multiplied by 10 to the power 1”, that is, -3.75. Similarly, 0.537 can be written as 5.37e-1, that is, “5.37 x 10”. The exponent can be specified using either e or E.
Note that there are several ways to write the same number. For example, the following all represent the same number 27.96:
27.96E00
2.796E1
2.796E+1
2.796E+01
0.2796E+02
279.6E-1
In Java, we can declare a floating-point variable using either float or double. A float value is normally stored as a 32-bit floating-point number, giving about 6 or 7 significant digits. A double value is stored as a 64-bit floating-point number, giving about 15 significant digits.
A floating-point constant is of type double unless it is followed by f or F, in which case it is of type float. Thus 3.75 is of type double but 3.75f (or 3.75F) is of type float. Most calculations are done using double precision. The type float is useful if you need to store lots of floating-point numbers and you wish to use as little storage as possible (and can live with 6 or 7 digits of precision).
In this series, we will mostly use double when working with floating-point numbers.