Floating-Point Literals
Floating-point data types come in two flavors: float and double.
The default data type of a floating-point literal is double, but it can be explicitly designated by appending the suffix D (or d) to the value. A floating-point literal can also be specified to be a float by appending the suffix F (or f).
Floating-point literals can also be specified in scientific notation, where E (or e) stands for exponent. For example, the double literal 194.9E-2 in scientific notation is interpreted as 194.9 × 10-2 (i.e., 1.949).
Examples of double Literals
0.0 0.0d 0D
0.49 .49 .49D
49.0 49. 49D
4.9E+1 4.9E+1D 4.9e1d 4900e-2 .49E2
Examples of float Literals
0.0F 0f
0.49F .49F
49.0F 49.F 49F
4.9E+1F 4900e-2f .49E2F
Note that the decimal point and the exponent are optional, and that at least one digit must be specified. Also, for the examples of float literals presented here, the suffix F is mandatory; if it were omitted, they would be interpreted as double literals.
Underscores in Numerical Literals
The underscore character (_) can be used to improve the readability of numerical literals in the source code. Any number of underscores can be inserted between the digits that make up the numerical literal. This rules out underscores adjacent to the sign (+, -), the radix prefix (0b, 0B, 0x, 0X), the decimal point (.), the exponent (e, E), and the data type suffix (l, L, d, D, f, F), as well as before the first digit and after the last digit. Note that octal radix prefix 0 is part of the definition of an octal literal and is therefore considered the first digit of an octal literal.
Underscores in identifiers are treated as letters. For example, the names _XXL and _XXL_ are two distinct legal identifiers. In contrast, underscores are used as a notational convenience for numerical literals and are ignored by the compiler when used in such literals. In other words, a numerical literal can be specified in the source code using underscores between digits, such that 2_0_2_2 and 20__22 represent the same numerical literal 2022 in source code.
Examples of Legal Use of Underscores in Numerical Literals
0b0111_1111_1111_1111_1111_1111_1111_1111
0_377_777_777 0xff_ff_ff_ff
-123_456.00 1_2.345_678e1_2
2009__08__13 49_03_01d
Examples of Illegal Use of Underscores in Numerical Literals
Click here to view code image _0_b_01111111111111111111111111111111_
_0377777777_ _0_x_ffffffff_
+_123456_._00_ _12_._345678_e_12_
_20090813_ _490301_d_