Java Notes


Floating-point numbers are like real numbers in mathematics, for example, 3.14159, -0.000001. Java has two kinds of floating-point numbers: float and double, both stored in IEEE-754 format. The default type when you write a floating-point literal is double.

Java floating-point types

typeSize RangePrecision
namebytesbits approximatein decimal digits
float 432 +/- 3.4 * 10386-7
double864 +/- 1.8 * 1030815

Limited precision

Because there are only a limited number of bits in each floating-point type, some numbers are inexact, just as the decimal system can not represent some numbers exactly, for example 1/3. The most troublesome of these is that 1/10 can not be represented exactly in binary.

Floating-point literals

There are two types of notation for floating-point numbers. Any of these numbers can be followed by "F" (or "f") to make it a float instead of the default double.

Positive and negative zero!

Because the representation of IEEE-754 floating point numbers has a separate sign bit, the sign of zero can be either positive or negative in principle, although all normal floating point operations produce a positive zero, so you don't have to pay attention to the difference as a practical matter. Interestingly, positive zero is regarded as greater than negative zero.


No exceptions are generated by floating-point operations. Instead of an interruption in execution, the result of an operation may be positive infinity, negative infinity, or NaN (not a number). Division by zero or overflow produce infinity. Subtracting two infinities produces a NaN. Use methods in the wrapper classes (Float or Double) to test for these values.

NaN (Not a Number) - Double.NaN and Float.NaN

If a floating point operation can't produce a value, the result is "Not a Number" (NaN).

Use methods in the wrapper classes (Float or Double) to test for these values.