Literal Formats for Integers - Example

This section provides a tutorial example on how to use integer literals in 4 formats: decimal, hex, octal and binary.

Now let's see a tutorial program example on integer literals in decimal, hex, octal and binary formats:

```/* IntegerLiteral.java
*/

class IntegerLiteral {
public static void main(String[] a) {
java.io.PrintStream o = System.out;
o.println("Decimal Integer Literals:");
o.println("   int value:              911  = "+911);
o.println("   int high:        2147483647  = "+2147483647);
o.println("   long value:      2147483648L = "+2147483648L);
o.println("   Using _:      2_147_483_648L = "+2_147_483_648L);

o.println();
o.println("Hex Integer Literals:");
o.println("   Hex format:           0x38F  = "+0x38F);
o.println("   Leading 0:       0x0000038F  = "+0x0000038F);
o.println("   int max value:   0x7FFFFFFF  = "+0x7FFFFFFF);
o.println("   long value:      0x7FFFFFFFL = "+0x7FFFFFFFL);
o.println("   int min value:   0x80000000  = "+0x80000000);
o.println("   long value:      0x80000000L = "+0x80000000L);
o.println("   int -1 value:    0xFFFFFFFF  = "+0xFFFFFFFF);
o.println("   long value:      0xFFFFFFFFL = "+0xFFFFFFFFL);

o.println();
o.println("Octal Integer Literals:");
o.println("   Octal format:         01617  = "+01617);
o.println("   int max value: 017777777777  = "+017777777777);
o.println("   long value:    017777777777L = "+017777777777L);
o.println("   int min value: 020000000000  = "+020000000000);
o.println("   long value:    020000000000L = "+020000000000L);
o.println("   Using _:     0200_0000_0000  = "+0200_0000_0000);
o.println("   long octal:  0200_0000_0000L = "+0200_0000_0000L);

o.println();
o.println("Binary Integer Literals:");
o.println("   Octal format:  0b1110001111  = "+0b1110001111);
o.println("   int min value:"
+" 0b1000_0000_0000_0000_0000_0000_0000_0000  = "
+0b1000_0000_0000_0000_0000_0000_0000_0000);
}
}
```

Compile this example program in a command window and run it:

```C:\herong>javac IntegerLiteral.java

C:\herong>java IntegerLiteral

Decimal Integer Literals:
int value:              911  = 911
int high:        2147483647  = 2147483647
long value:      2147483648L = 2147483648
Using _:      2_147_483_648L = 2147483648

Hex Integer Literals:
Hex format:           0x38F  = 911
int max value:   0x7FFFFFFF  = 2147483647
long value:      0x7FFFFFFFL = 2147483647
int min value:   0x80000000  = -2147483648
long value:      0x80000000L = 2147483648
int -1 value:    0xFFFFFFFF  = -1
long value:      0xFFFFFFFFL = 4294967295

Octal Integer Literals:
Octal format:         01617  = 911
int max value: 017777777777  = 2147483647
long value:    017777777777L = 2147483647
int min value: 020000000000  = -2147483648
long value:    020000000000L = 2147483648
Using _:     0200_0000_0000  = -2147483648
long octal:  0200_0000_0000L = 2147483648

Binary Integer Literals:
Octal format:  0b1110001111  = 911
int min value: 0b1000_0000_0000_0000_0000_0000_0000_0000
= -2147483648
```

Hope you know how to use integer literals properly now.

Last update: 2014.