41

I am studying for java certification. And i'm curious about the java literals. I know it is possible to do something like this:

int i = 0xAA;
long l = 0xAAL;

Also this is possible for floating-point variables:

double d = 123d;
float f = 123f;

So I logically thought with these examples that the same would apply for hexadecimal. Just like i can add L for long literals, I could add 'd' or 'f' but the logic is flawed since 'F' and 'D' are valid hexadecimal values.

It is not possible to do something like this:

double d = 0xAAAAAAAAAAAAAAAAAAd;

Is this just not allowed by Java or there is a simple way to do it that I don't know?

Bruno Calça
  • 750
  • 1
  • 7
  • 16
  • Do you understand IEEE-754 floating point format? Did you study the Java language reference documentation? What do you expect is the floating point value of `0xaaaaaaaaaaaaaaaa`? – Jim Garrison Mar 10 '17 at 16:20
  • 6
    @JimGarrison: The OP says "It is not possible" which suggests they have indeed tried and found it gives an error. I wasn't aware of floating-point hex literals in Java myself until I started answering the question. – Jon Skeet Mar 10 '17 at 16:22
  • @JonSkeet that's interesting.... – Jim Garrison Mar 10 '17 at 16:23
  • 1
    @JimGarrison Yes, I have studied the java documentation, but I didn't try this specific case, since the exam may ask anything that is possible or not, I had to understand that too. I had the notion about the floating point format, but thanks for the IEEE reference. – Bruno Calça Mar 10 '17 at 16:52

1 Answers1

61

It turns out it is possible, although that surprised me. Section 3.10.2 of the JLS gives the structure of floating point literals, including HexadecimalFloatingPointLiteral.

public class Test {

    public static void main(String[] args) {
        double d1 = 0xAAAAAAAAAAAAAAAAAAp0d;
        double d2 = 0x1.8p1d;

        System.out.println(d1); // A very big number
        System.out.println(d2); // 24 = 1.5 * 2^1
    }
}

The p is required as part of the binary exponent - the value after the p is the number of bits to shift the value left. Other examples:

0x1.4p0d => 1.25 (binary 0.01 shifted 0 bits)
0x8p-4d => 0.5 (binary 1000 shifted *right* 4 bits)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It turns out to be a quite difficult-to-read literal, but it is possible indeed! – Bruno Calça Mar 10 '17 at 16:47
  • 4
    C has hexadecimal floating-point constants, with similar syntax, added in C99. – Keith Thompson Mar 10 '17 at 17:40
  • 2
    @KeithThompson -- yeah, the syntax was originally adopted for C99 AFAICT and then migrated to other languages in the C-family as well as Python and some others from there. (They're useful in low-level numerics code where bitwise-exact FP values are needed.) – LThode Mar 10 '17 at 21:57
  • 4
    And in case it's not obvious. hexadecimal floating-point uses `p` (perhaps for "point"?) to introduce the exponent (rather than the `e` used for decimal floating-point) because `e` is also a hexadecimal digit. And the exponent part is mandatory to avoid ambiguity with an `f` suffix used to indicate a constant of type `float`. – Keith Thompson Mar 10 '17 at 23:58
  • Great answer. Much more helpful than Jim Garrison's trolly whine. Thank you! – Midiman Jan 28 '21 at 09:59