Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0xp0 prints 0.0 (Hexadecimal Floating Point Literal)

Tags:

java

I just wonder why this compiles? and what does it mean since it does compile?

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

OUTPUT:

0.0
like image 692
Eng.Fouad Avatar asked Jul 25 '12 14:07

Eng.Fouad


2 Answers

The JLS explains it:

HexadecimalFloatingPointLiteral:
    HexSignificand BinaryExponent FloatTypeSuffixopt

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

BinaryExponent:
    BinaryExponentIndicator SignedInteger

BinaryExponentIndicator:one of
    p P

Based on the above, I would expect a mandatory .HexDigit before the p, though.

like image 160
JB Nizet Avatar answered Nov 08 '22 08:11

JB Nizet


It's a floating point hex literal.

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.

See the specification here.

like image 23
Jeff Foster Avatar answered Nov 08 '22 10:11

Jeff Foster