## 1.2.  Use binary literals, numeric literals with underscores [JAVA_TUTORIAL_PRIMITIVES] Primitive Data Types [BINARY_LITERALS] Binary Literals

Integer Literals

An integer literal is of type `long` if it ends with the letter 'L' or 'l'; otherwise it is of type `int`. It is recommended that you use the upper case letter 'L' because the lower case letter 'l' is hard to distinguish from the digit 1.

Values of the integral types `byte`, `short`, `int`, and `long` can be created from `int` literals. Values of type `long` that exceed the range of `int` can be created from `long` literals. Integer literals can be expressed these number systems:

• Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day

• Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F

• Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later)

For general-purpose programming, the decimal system is likely to be the only number system you will ever use. However, if you need to use another number system, the following example shows the correct syntax. The prefix '0x' indicates hexadecimal and '0b' indicates binary:

```int decVal = 26;      // The number 26, in decimal
int hexVal = 0x1a;    // The number 26, in hexadecimal
int binVal = 0b11010; // The number 26, in binary
```

Binary Literals

In Java SE 7, the integral types (`byte`, `short`, `int`, and `long`) can also be expressed using the binary number system. To specify a binary literal, add the prefix `0b` or `0B` to the number. The following examples show binary literals:

```// An 8-bit 'byte' value:
byte aByte = 0b00100001;

// A 16-bit 'short' value:
short aShort = 0b0010001010001010;

// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.

// A 64-bit 'long' value. Note the "L" suffix:
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

// OK, implicit casting int to float or double variable
float  f1 = 0b0001;
double d1 = 0b0001;

// COMPILATION FAILS! Binary literal can only be of integer type (byte, char, int, long),
// while 'd' or 'f' suffixes explicitly say these are decimal point type literals
double d2 = 0b0001d;
float  f2 = 0b0001f;
```

Underscores in Numeric Literals

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

The following example shows other ways you can use the underscore in numeric literals:

```long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
```

You can place underscores only between digits; you CANNOT place underscores in the following places:

• At the beginning or end of a number

• Adjacent to a decimal point in a floating point literal

• Prior to an 'F' or 'L' suffix

• In positions where a string of digits is expected

The following examples demonstrate valid and invalid underscore placements in numeric literals.

Valid:

```int x1 = 5_2;              // OK (decimal literal)
int x2 = 5_______2;        // OK (decimal literal)
int x3 = 0x5_2;            // OK (hexadecimal literal)
int x4 = 0B0_0_0;          // OK (binary literal)
```

Invalid:

```float pi1 = 3_.1415F;       // Cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F;       // Cannot put underscores adjacent to a decimal point
long ssn  = 999_99_9999_L;  // Cannot put underscores prior to an L suffix

int x1 = 52_;               // Cannot put underscores at the end of a literal
int x2 = 0_x52;             // Cannot put underscores in the 0x radix prefix
int x3 = 0x_52;             // Cannot put underscores at the beginning of a number
int x4 = 0x52_;             // Cannot put underscores at the end of a number
```

Invalid (compilable, but logical error):

```int x1 = _52;              // This is an identifier, not a numeric literal
```    