Java: Number Literals

By Xah Lee. Date:

Numbers can be written in Java as decimals, hexidecimal, or octal.

To input in octal notation, prefix with 0.

To input in hexdecimal notation, prefix with 0x. Letters A to F can be either lower case or upper case.

A integer literal is of type “long” if it is suffixed with L or lower case l; otherwise it is of type “int”.

class Test {
    public static void main(String[] arg) {
        int x1 = 10; // 10 in decimal
        int x2 = 012; // 10 in octal
        int x3 = 0xA; // 10 in hexdecimal

        System.out.println( x1 );
        System.out.println( x2 );
        System.out.println( x3 );
    }
}

If a number is written with the decimal point, it is automatically of type “double”.

You can append f to a number to indicate it's a type “float”. Similarly, d for “double”.

Examples of float literals:
1e1f    2.f     .3f     0f      3.14f   6.022137e+23f

Examples of double literals:

1e1     2.      .3      0.0     3.14    1e-9d   1e137

Reference

http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html