2012-07-25 20 views
17

Mi chiedo solo perché questo compila? e cosa significa da quando viene compilato?0xp0 stampa 0,0 (letterale punto esadecimale letterale)

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

USCITA:

0.0 
+0

Per me questo non viene compilato, con 'javac 1.7.0_02'. Solo '0x0p0' fa. –

+0

@TheGuyOfDoom Sto usando '1.7.0_05'. –

+0

Sono a proposito sul Sun JDK. potrebbe essere rilevante. –

risposta

9

È un letterale esadecimale a virgola mobile.

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.

Vedere la specifica here.

+0

+1 Bene, la prima volta che conosco l'esagono in virgola mobile. –

11

The JLS lo spiega:

HexadecimalFloatingPointLiteral: 
    HexSignificand BinaryExponent FloatTypeSuffixopt 

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

BinaryExponent: 
    BinaryExponentIndicator SignedInteger 

BinaryExponentIndicator:one of 
    p P 

Sulla base di quanto sopra esposto, mi aspetterei un obbligo .HexDigit prima della p, però.

0

Solo per riferimento, ecco come convertire manualmente il seguente in un numero decimale:

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

enter image description here