2012-07-25 64 views
17

我只是想知道为什么编译?这是什么意思,因为它编译?0xp0打印0.0(十六进制浮点文字)

System.out.println(0xp0); // p? 

OUTPUT:

0.0 
+0

对于我来说,这并不编译,以'javac的1.7.0_02'。只有'0x0p0'。 – 2012-07-25 14:14:14

+0

@TheGuyOfDoom我正在使用'1.7.0_05'。 – 2012-07-25 14:15:47

+0

我在Sun JDK上很顺利。这可能是相关的。 – 2012-07-25 14:16:40

回答

9

这是一个浮点十六进制文字。

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.

参见规格here

+0

+1那么,我第一次知道浮点十六进制。 – 2012-07-25 14:21:48

11

The JLS解释它:基于上述

HexadecimalFloatingPointLiteral: 
    HexSignificand BinaryExponent FloatTypeSuffixopt 

HexSignificand: 
    HexNumeral 
    HexNumeral . 
    0 x HexDigitsopt . HexDigits 
    0 X HexDigitsopt . HexDigits 

BinaryExponent: 
    BinaryExponentIndicator SignedInteger 

BinaryExponentIndicator:one of 
    p P 

,我预计p前强制性.HexDigit,虽然。

0

仅供参考,这里是如何手动转换以下为十进制数:

double hfpl = 0x1e.18p4; 
System.out.println(hfpl); // 481.5 

enter image description here