Java SE 7 Feature: A Numeric Underscores with Binary Literals


Hello folks,

     In this tutorial, we will discuss about underscore ( _ ) in Literals feature added in Java SE7. Prior to Java SE 7, declaring numeric literals with underscores interspersed within the digits would causes a compile time error. But in JDK7, numeric literals with underscore characters are not only legal, but they’re highly encouraged to increase readability of code. This is a nice and small feature.

Underscore can be added anywhere in the value part with few constraints listed below,

Rules for using Numeric number with Underscores in Java 7
  1. Underscores ( _ ) can’t go at the beginning or the end of a number.
  2. You can’t use an underscore on either side of a decimal. 
  3. The underscore cannot go before an identifying suffix such as F, D or L
  4. You can’t put an underscore before or after the binary or hexadecimal identifiers b and x.
Example of valid 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;
Example of invalid literals with reason:
int a1     = _52;                             // This is an identifier, not a numeric literal
int a2     = 52_;                             // cannot put underscores at the end of a literal
int a3     = 0_x52;                         // cannot put underscores in the 0x radix prefix
int a4     = 0x_52;                         // cannot put underscores at the beginning of a number
float pi   = 3._1415F;                   // cannot put underscores adjacent to a decimal point
long ssn = 999_99_9999_L;        // cannot put underscores prior to an L suffix

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. This makes it easier to read code which uses bitwise operations. The following examples show binary literals:

int x = Integer.parseInt(“1000”, 2); //previously, you would have had to do it like this:

int eight = 0b1000; //in jdk7, you can create a binary literal like this:

int four = 0b1000>>1;      //easier to read bitwise operations

There is not limit in underscore. You can use as many underscore as you want. Only reason I see to use any number of underscores, is to be able to do fancy stuff like in the following piece of code (created by Joshua Bloch, if I’m not mistaken):

private static final int BOND =
     0000_____________0000________0000000000000000__000000000000000000+
   00000000_________00000000______000000000000000__0000000000000000000+
  000____000_______000____000_____000_______0000__00______0+
 000______000_____000______000_____________0000___00______0+
0000______0000___0000______0000___________0000_____0_____0+
0000______0000___0000______0000__________0000___________0+
0000______0000___0000______0000_________0000__0000000000+
0000______0000___0000______0000________0000+
 000______000_____000______000________0000+
  000____000_______000____000_______00000+
   00000000_________00000000_______0000000+
     0000_____________0000________000000007;

FEATURE SUMMARY:

Java numeric literals will allow underscores to be placed in (nearly) arbitrary positions within the number, at the programmer’s discretion, for readability purposes. These underscores shall be ignored by the compiler for the purposes of code generation.

MAJOR BENEFIT:

  1. Increased readability of code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s