See §4.2.3 for a general discussion of the floating-point types and values.
A floating-point literal has the following parts: a whole-number part, a decimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix. The exponent, if present, is indicated by the ASCII letter e
or E
followed by an optionally signed integer.
At least one digit, in either the whole number or the fraction part, and either a decimal point, an exponent, or a float type suffix are required. All other parts are optional.
A floating-point literal is of type float
if it is suffixed with an ASCII letter F
or f
; otherwise its type is double
and it can optionally be suffixed with an ASCII letter D
or d
.
FloatingPointLiteral:
Digits.
DigitsoptExponentPartopt
FloatTypeSuffixopt
.
DigitsExponentPartopt
FloatTypeSuffixopt
DigitsExponentPart
FloatTypeSuffixopt
DigitsExponentPartopt
FloatTypeSuffix ExponentPart:
ExponentIndicatorSignedInteger ExponentIndicator: one of
e E
SignedInteger:
SignoptDigits Sign: one of
+ -
FloatTypeSuffix: one of
f F d D
The Java types float
and double
are IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point values, respectively.
The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf
of class Float
(§20.9.17) and class Double
(§20.10.16) of the package java.lang
.
The largest positive finite float
literal is 3.40282347e+38f
. The smallest positive finite nonzero literal of type float
is 1.40239846e-45f
. The largest positive finite double
literal is 1.79769313486231570e+308
. The smallest positive finite nonzero literal of type double
is 4.94065645841246544e-324
.
See Float.MIN_VALUE
(§20.9.1) and Float.MAX_VALUE
(§20.9.2); see also Double.MIN_VALUE
(§20.10.1) and Double.MAX_VALUE
(§20.10.2).
A compile-time error occurs if a nonzero floating-point literal is too large, so that on rounded conversion to its internal representation it becomes an IEEE 754 infinity. A Java program can represent infinities without producing a compile-time error by using constant expressions such as 1f/0f
or -1d/0d
or by using the predefined constants POSITIVE_INFINITY
and NEGATIVE_INFINITY
of the classes Float
(§20.9) and Double
(§20.10).
A compile-time error occurs if a nonzero floating-point literal is too small, so that, on rounded conversion to its internal representation, it becomes a zero. A compile-time error does not occur if a nonzero floating-point literal has a small value that, on rounded conversion to its internal representation, becomes a nonzero denormalized number.
Predefined constants representing Not-a-Number values are defined in the classes Float
and Double
as Float.NaN
(§20.9.5) and Double.NaN
(§20.10.5).
1e1f 2.f .3f 0f 3.14f 6.022137e+23f
1e1 2. .3 0.0 3.14 1e-9d 1e137
There is no provision for expressing floating-point literals in other than decimal radix. However, method intBitsToFloat
(§20.9.23) of class Float
and method longBitsToDouble
(§20.10.22) of class Double
provide a way to express floating-point values in terms of hexadecimal or octal integer literals. For example, the value of:
Double.longBitsToDouble(0x400921FB54442D18L)
is equal to the value of Math.PI
(§20.11.2).