my name is GYAN
my name is GYAN

Reputation: 1289

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.

Upvotes: 0

Views: 311

Answers (1)

Henry
Henry

Reputation: 43738

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

Upvotes: 1

Related Questions