Floating-Point Literals
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, of type double if it is suffixed with an ASCII letter D or d; otherwise its type is BigDecimal and must be suffixed with the ASCII letters FB or fb.
FloatingPointLiteral:
Digits . Digitsopt ExponentPartopt FloatTypeSuffixopt
. Digits ExponentPartopt FloatTypeSuffixopt
Digits ExponentPart FloatTypeSuffixopt
Digits ExponentPartopt FloatTypeSuffix
ExponentPart:
ExponentIndicator SignedInteger
ExponentIndicator: one of
e E
SignedInteger:
Signopt Digits
Sign: one of
+ -
FloatTypeSuffix: one of
f F d D fb FB
The elements of the types float and double are those values that can be represented using the IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point formats, respectively.
The details of proper input conversion from an ASCII 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 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.
If a nonzero floating-point literal of type float is too large or too small, the value is then represented as a floating-point literal of type double.
If a nonzero floating-point literal of type double is too large, the value is then represented as a floating-point literal of type BigDecimal.
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:
1e1d 2.D .3d 0.0D 3.14D 1e-9d 1e137
Examples of BigDecimal literals:
1e1fb 2.FB .3fb 0.0FB 3.14FB 1e-9dfb 1e133334217
There is no provision for expressing floating-point literals in other than decimal radix. However, method intBitsToFloat of class Float and method longBitsToDouble 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.
Each float, double, and Double literal is a reference to an instance of class Float, Double and BigDecimal respectively. These objects have a constant value. The Float and Double objects can be used interchangeably with their counter part Java primitive data types when calling methods that expect the primitive types or when accessing Java attributes declared using the Java primitive data type.