PROPOSAL: Underscores in Numbers (Version 2)

Derek Foster vapor1 at
Thu Apr 30 21:01:35 PDT 2009

Per feedback on the Project Coin mailing list, the following proposal has been revised to disallow placing underscores at the beginning or end of a sequence of digits within a number. I have also revised the value used for the "alsoMaxLong" example (as it was previously illegal), as well as the other examples showing where underscores can/cannot be placed.

AUTHOR(S): Derek Foster


In Java, currently, numbers of various types currently are expressed in their pure form, as a long string of digits possibly interspersed with other punctuation (periods, an exponent specifier, etc.) needed to separate distinct sections of the number. While this is easy for a compiler to process, it is often difficult for a human being to visually parse.

The ability of a human to visually separate separate items tops out somewhere near "seven plus or minus two" items. Research done by telephone companies suggests that for many practical purposes, the longest string of numbers an average human can successfully hold in memory at a time is around three or four. Also, it is difficult for the human eye to find common positions in numbers that have no break in their visual structure.

As a result, most numbers that humans deal with in day-to-day life have separators included in them to break the long sequence of digits into manageable chunks that are easy to deal with as separate entities. This includes items such as (apologies to non-USA readers...):

Phone numbers:           555-555-1212
Credit card numbers:     1234-5678-9012-3456
Social security numbers: 999-99-9999
Monetary amounts:        $12,345,132.12

and a wide variety of other types of numbers.

However, Java provides no way to add these kinds of visual separators into a number. Java expects the number to be essentially an unbroken string of digits.

This proposal suggests that Java follow the lead of the Ruby programming language in allowing the underscore character to be inserted into numbers in most positions, for readability purposes.


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.


Programmers won't have to visually parse long strings of digits (a task humans are quite poor at). The internal digit-oriented structure of many numbers can be made more clear.


Increased readability of code.


The number parsers in the Java compiler would have to be adjusted to parse and ignore the underscores. This is a small amount of effort, but nonzero. There might also be some small performance impact.

If someone were to use this feature inappropriately, it could result in difficult to read code.


Do without separators in numbers, or use some other character for them.


SIMPLE EXAMPLE: Show the simplest possible program utilizing the new feature.

int phoneNumber = 555_555_1212;


long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumbers = 999_99_9999L;
float monetaryAmount = 12_345_132.12;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xFFEC_DE5E;
long maxLong = 0x7fff_ffff_ffff_ffffL;
long alsoMaxLong = 9_223_372_036_854_775_807L;
double whyWouldYouEverDoThis = 0x1_.ffff_ffff_ffff_fp10_23;

(Additionally, if binary literals are ever added to the Java language, the following might also be possible...
   byte nybbles = 0b0010_0101;
   long bytes = 0b11010010_01101001_10010100_10010010;
   int weirdBitfields = 0b000_10_101;

Note that according to this proposal, underscores can only be placed between digits. They cannot be placed by themselves in positions where a string of digits would normally be expected:

int x1 = _52;  // This is an identifier, not a numeric literal.
int x2 = 5_2;  // OK. (Decimal literal)
int x2 = 52_;  // Illegal. (Underscores must always be between digits)
int x3 = 5_______2; // OK. (Decimal literal.)

int x4 = 0_x52;  // Illegal. Can't put underscores in the "0x" radix prefix.
int x5 = 0x_52;  // Illegal. (Underscores must always be between digits)
int x6 = 0x5_2;  // OK. (Hexadecimal literal)
int x6 = 0x52_;  // Illegal. (Underscores must always be between digits)
int x6 = 0x_;    // Illegal. (Not valid with the underscore removed)

int x7 = 0_52;   // OK. (Octal literal)
int x7 = 05_2;   // OK. (Octal literal)
int x8 = 052_;   // Illegal. (Underscores must always be between digits)




Section 3.10.1 ("Integer Literals") of the Java Language Specification 3rd edition shall be modified like so:

            _ Underscores

            NonZeroDigit Underscores_opt DigitsAndUnderscores_opt

            DigitsAndUnderscores Underscores_opt Digit


    NonZeroDigit: one of
            1 2 3 4 5 6 7 8 9

        0 x HexDigitsAndUnderscores
        0 X HexDigitsAndUnderscores

        HexDigit Underscores_opt HexDigitsAndUnderscores

    HexDigit: one of
            0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

            0 OctalDigitsAndUnderscores

            OctalDigit Underscores_opt OctalDigitsAndUnderscores

    OctalDigit: one of
            0 1 2 3 4 5 6 7

Section 3.10.2 ("Floating-Point Literals") would be modified as follows:


          DigitsAndUnderscores_opt . DigitsAndUnderscores_opt ExponentPart_opt FloatTypeSuffix_opt
          . DigitsAndUnderscores ExponentPartopt FloatTypeSuffix_opt
          DigitsAndUnderscores_opt ExponentPart FloatTypeSuffix_opt
          DigitsAndUnderscores_opt ExponentPart_opt FloatTypeSuffix

            ExponentIndicator SignedInteger

    ExponentIndicator: one of
            e E

            Sign_opt DigitsAndUnderscores

    Sign: one of
            + -

    FloatTypeSuffix: one of
            f F d D

            HexSignificand BinaryExponent FloatTypeSuffix_opt

            HexNumeral .
            0x HexDigitsAndUnderscores_opt . HexDigitsAndUnderscores
            0X HexDigitsAndUnderscores_opt . HexDigitsAndUnderscores

            BinaryExponentIndicator SignedInteger

    BinaryExponentIndicator:one of
            p P


Numbers containing underscores are to be parsed and evaluated by the compiler exactly as if the underscores were not present. The above grammar ensures that removing underscores will not result in an unparseable number.

A simple strategy for achieving this is that once a number has been parsed by the compiler lexer and determined to be syntactically valid according to the above grammar, then if the number contains any underscores, then all underscores in it may be removed (by something as simple as numberAsString.replaceAll("_","")) before passing the number on to the code that would normally have parsed the number prior to this proposal.

More performant approaches are certainly possible.

TESTING: How can the feature be tested?

A variety of literals may be generated, of the cross product of each of the following radicies:

    hex, decimal, octal

with each of the following types:

    byte, char, short, int, long, float, double

such that for each possible numeric field in the result, that one or more underscores are inserted at the beginning, in the middle, and at the end of the digits.

Note that the above grammar is specifically designed to disallow any underscores from appearing which are not either preceded by or followed by a digit.


Methods such as Integer.decode(String) and Long.decode(String) should probably be updated to ignore underscores in their inputs, since these methods attempt to parse according to Java conventions.

I suggest that methods such as Integer.parseInt(), Float.parseFloat(), etc. should probably NOT be updated to ignore underscores, since these methods deal with numbers in their pure form, and are more focused and much more widely used. To alter them to ignore underscores would introduce ambiguity in (and have a performance impact on) various parsing code that uses them.


No changes to reflective APIs are needed.


No other changes are needed.


Underscores can be inserted into numbers within an existing code base as desired for readability.



Since use of underscores within numbers was previously a syntax error, this should not break any existing programs.


This feature does not affect the format of class files. It is purely a notational convenience. Hence, interaction with existing class files would not be affected.



A search of the Bug Database did not find any bug ID's related to this proposal.



More information about the coin-dev mailing list