A floating-point literal has the following parts: a whole-number part, a decimal or hexadecimal point (represented by an ASCII period character), a fraction part, an exponent, and a type suffix.
A floating-point literal may be expressed in decimal (base 10) or hexadecimal (base 16).
For decimal floating-point literals, 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. The exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer.
For hexadecimal floating-point literals, at least one digit is required (in either the whole number or the fraction part), and the exponent is mandatory, and the float type suffix is optional. The exponent is indicated by the ASCII letter p or P followed by an optionally signed integer.
Underscores are allowed as separators between digits that denote the whole-number part, and between digits that denote the fraction part, and between digits that denote the exponent.
FloatingPointLiteral:
DecimalFloatingPointLiteral
HexadecimalFloatingPointLiteral
DecimalFloatingPointLiteral:
Digits . [Digits] [ExponentPart] [FloatTypeSuffix]
. Digits [ExponentPart] [FloatTypeSuffix]
Digits ExponentPart [FloatTypeSuffix]
Digits [ExponentPart] FloatTypeSuffix
ExponentPart:
ExponentIndicator SignedInteger
ExponentIndicator:
(one of)
e E
SignedInteger:
[Sign] Digits
Sign:
(one of)
+ -
FloatTypeSuffix:
(one of)
f F d D
HexadecimalFloatingPointLiteral:
HexSignificand BinaryExponent [FloatTypeSuffix]
HexSignificand:
HexNumeral [.]
0 x [HexDigits] . HexDigits
0 X [HexDigits] . HexDigits
BinaryExponent:
BinaryExponentIndicator SignedInteger
BinaryExponentIndicator:
(one of)
p P
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.
The elements of the types float and double are those values that can be represented using the IEEE 754 binary32 and IEEE 754 binary64 floating-point formats, respectively (§4.2.3).
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 and class Double of the package java.lang.
The largest and smallest positive literals of type float are as follows:
-
The largest positive finite
floatvalue is numerically equal to (2 - 2^{-23}) ⋅ 2^127.The shortest decimal literal which rounds to this value is
3.4028235e38f.A hexadecimal literal for this value is
0x1.fffffeP+127f. -
The smallest positive finite non-zero
floatvalue is numerically equal to 2^{-149}.The shortest decimal literal which rounds to this value is
1.4e-45f.Two hexadecimal literals for this value are
0x0.000002P-126fand0x1.0P-149f.
The largest and smallest positive literals of type double are as follows:
-
The largest positive finite
doublevalue is numerically equal to (2 - 2^{-52}) ⋅ 2^1023.The shortest decimal literal which rounds to this value is
1.7976931348623157e308.A hexadecimal literal for this value is
0x1.f_ffff_ffff_ffffP+1023. -
The smallest positive finite non-zero
doublevalue is numerically equal to 2^{-1074}.The shortest decimal literal which rounds to this value is
4.9e-324.Two hexadecimal literals for this value are
0x0.0_0000_0000_0001P-1022and0x1.0P-1074.
It is a compile-time error if a non-zero floating-point literal is too large, so that on rounded conversion to its internal representation, it becomes an IEEE 754 infinity.
A 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 and Double.
It is a compile-time error if a non-zero 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 non-zero floating-point literal has a small value that, on rounded conversion to its internal representation, becomes a non-zero subnormal number.
Predefined constants representing Not-a-Number values are defined in the classes Float and Double as Float.NaN and Double.NaN.
Examples of float literals:
1e1f 2.f .3f 0f 3.14f 6.022137e+23f
Examples of double literals:
1e1 2. .3 0.0 3.14 1e-9d 1e137