简体   繁体   中英

How we can write binary, octal and hexadecimal literals of floating point numbers in Java?

How we can write binary, octal and hexadecimal literals of floating point numbers in Java? Either in scientific or in normal representation.

class First
{
        public static void main(String[] args)
        {
                double b = 0x12p3;
                System.out.println(b);
        }
}

The above program results 144.0

class First
{
        public static void main(String[] args)
        {
                double b = 0x12e3;
                System.out.println(b);
        }
}

The above program results 4835.0

class First
{
            public static void main(String[] args)
            {
                    double b = 0x12e3;
                    System.out.println(b);
            }
}

The above program results 4835.0

class First
{
            public static void main(String[] args)
            {
                    double b = 012e3;
                    System.out.println(b);
            }
}

The above program results 12000

Please explain above outputs.

The first example is a HexadecimalFloatingPointLiteral, (16+2)*8 = 144.

The second and third example is actually a HexIntegerLiteral assigned to a double variable, 1*16*16*16 + 2*16*16 + 14*16 + 3 = 4835. Note that the 'e' is just the hex digit for 14.

The last example is a DecimalFloatingPointLiteral, 12 * 1000.

Java does not support an octal floating point literals.

For all the details see the JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM