Java specification ,它说了以下内容:
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.
据我了解,指示字母的指数必须是 p 或 P 才能解决与十六进制数字 e 或 E 的歧义。为什么它说后缀指示类型(float vs double)是可选的,当使用它时会用同样是十六进制数字的字母d、D、f、F引入歧义?
最佳答案
根据grammar :
DecimalFloatingPointLiteral: Digits . [Digits] [ExponentPart] [FloatTypeSuffix] . Digits [ExponentPart] [FloatTypeSuffix] Digits ExponentPart [FloatTypeSuffix] Digits [ExponentPart] FloatTypeSuffix HexadecimalFloatingPointLiteral: HexSignificand BinaryExponent [FloatTypeSuffix]
可选的 FloatTypeSuffix
必须 遵循十六进制 float 的强制性 BinaryExponent
。
如果添加f
或d
而没有BinaryExponent
,则它是十进制 float 。
关于Java 十六进制浮点字面量歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51026405/