Hexadecimal Octal and Binary literals

suggest change

A hexadecimal number is a value in base 16. There are 16 digits, 0-9 and the letters A-F (case does not matter). A through F represent 10 through 15, not 16. Sixteen is 0x10.

An octal number is a value in base-8, and uses the digits 0-7.

A binary number is a value in base-2, and uses the digits 0 and 1.

All of these numbers result in the same value, 110:

int dec = 110;            // no prefix   --> decimal literal
int bin = 0b1101110;      // '0b' prefix --> binary literal
int oct = 0156;           // '0' prefix  --> octal literal
int hex = 0x6E;           // '0x' prefix --> hexadecimal literal

Note that binary literal syntax was introduced in Java 7.

The octal literal can easily be a trap. A leading 0 on an integer literal makes it octal, so the value is not the decimal number those digits suggest. A leading zero does not do this to a floating-point literal: 0100.0 is the decimal double 100.0.

int a = 0100;        // Instead of 100, a == 64

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents