Literal Formats for Floating-Point

This section describes 2 literal formats for floating-point values: decimal and hex floating-point literals. The default floating-point literal data type is 'double'.

Java supports literals for 2 floating-point data types in 2 formats described below:

1. Decimal Floating-Point Literal - Basic rules on decimal floating-point literals:

• A floating-point value in "double" data type can be represented 2 parts: a base part and an optional exponent part.
• The base part consists of an integer part, a decimal point and a fractional part. Examples are: 365.242196, 3.14159 and 0.000000001.
• The exponent part is a sequence of decimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 with a leading sign + or - and a prefix of e or E. Examples are: 3.65242196e2, 3.14159e0 and 1.0e-9.
• To specify values in "float" data type, suffix F or f can be added. Examples are: 3.65242196e2F, 3.14159e0F and 1.0e-9F.
• To increase readability, _ can be used as group separators. Examples are: 3.652_421_96e2F and 3.141_59e0F.

2. Hex Floating-Point Literal - Basic rules on hex floating-point literals:

• A floating-point value in "double" data type can be represented 2 parts: a base part and an exponent part.
• The base part consists of an integer part, a decimal point, a fractional part and a prefix of 0x or 0X. The base part should be evaluated as a hexadecimal number: each digit in the integer part is evaluated 16 times more than the next digit on the decimal point side. each digit in the fractional part is evaluated 16 times less than the next digit on the decimal point side. For example: 0x123.456 should be evaluated as 1*16*16 + 2*16 + 3 + 4/16 + 5/16/16 + 6/16/16.
• The exponent part is a sequence of decimal digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 with a leading sign + or - and a prefix of p or P. The exponent part is evaluated with a base of 2, not 10. For example: 0x1p4 should be evaluated as 1*2*2*2*2.
• To specify values in "float" data type, suffix F or f can be added. Examples are: 0x123.456p0F and 0x1.23456p8F.
• To increase readability, _ can be used as group separators. Examples are: 0x12_345.6p-8F and 0x1.23_456p8F.