Skip to content

Instantly share code, notes, and snippets.

@VijayKrishna
Created September 5, 2013 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VijayKrishna/6447013 to your computer and use it in GitHub Desktop.
Save VijayKrishna/6447013 to your computer and use it in GitHub Desktop.
Using Underscore Characters in Numeric Literals; From http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Using Underscore Characters 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 (which are highlighted) in numeric literals:
// Invalid: cannot put underscores
// adjacent to a decimal point
float pi1 = 3_.1415F;
// Invalid: cannot put underscores
// adjacent to a decimal point
float pi2 = 3._1415F;
// Invalid: cannot put underscores
// prior to an L suffix
long socialSecurityNumber1 = 999_99_9999_L;
// This is an identifier, not
// a numeric literal
int x1 = _52;
// OK (decimal literal)
int x2 = 5_2;
// Invalid: cannot put underscores
// At the end of a literal
int x3 = 52_;
// OK (decimal literal)
int x4 = 5_______2;
// Invalid: cannot put underscores
// in the 0x radix prefix
int x5 = 0_x52;
// Invalid: cannot put underscores
// at the beginning of a number
int x6 = 0x_52;
// OK (hexadecimal literal)
int x7 = 0x5_2;
// Invalid: cannot put underscores
// at the end of a number
int x8 = 0x52_;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment