8

Java 7+ allows to use underscores in numeric literals,which do not affect the value of the literal, yet are useful for grouping. Examples from the Java 7 documentation, entitled "Underscores 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;
  • here is the doc for Java 8, I haven't for anything like this for later versions;
  • here is the relevant section in the Java Language Specification v. 13.

These days, this feature can also be found in Python and others.

Which (if any) languages prior to Java 7 (released July 2011) had this particular feature?

  • 4
    To the within-seconds downvoter: If there is a "history" tag for "software engineering" and stackoverflow specifically says "do not use 'history' tag here, go somehwere else", this is probably the right place. "Software Engineering" includes history. Actually, it must! – David Tonhofer Jan 17 at 9:32
  • 3
    I've no idea which was the first language to have this, but Ada 83 definitely supported underscores. It was based on an obscure French language called LIS, so that too may have provided underscores in numeric literals in the 1970's, but that's a guess. – David Arno Jan 17 at 9:46
  • 2
    @Christophe, my comment isn't a definitive answer. For example, the language XPL0 dates from the 70's (ie before Ada) and it too supports underscores in numbers. But I've no idea when they were added to the language. ALGOL 68 allowed spaces in numbers. So my guess is that at some stage in the 70s, someone hit upon the idea of using underscores instead as that would have simplified the parser. Which language was first though is likely to prove very hard to determine exactly. – David Arno Jan 17 at 10:36
  • 2
    Read down a little further and it says "In fact underlines can be inserted into any number, such as $12_34, or -10_000. The underlines are simply ignored by the compilers.". But as you say it's a 2012 manual. I can't determine whether XPL0 in the 70s supported this. So Ada remains the only language I can date as to when it supported this feature (1983). It is unlikely to be the first though, so still isn't a proper answer to your question. – David Arno Jan 17 at 11:45
  • 2
    @JoopEggen Commenting is good! I hope you are posting from a mountain cabin. Oh, a new grammar class. Most excellent. – David Tonhofer Jan 17 at 15:54
11

In absence of an answer of David Arno, who first emitted the hypothese, here a summary of the findings.

It appears that the first programming language that offered underscores as digit separator in numeric literals was Ada. No evidence of a prior use in another language was found.

Its use in Ada & historical evidence

In the paper *The rationale of the design of the Ada programming language, published in June 1979 by Ada lead designer Jean Ichbiah et al., the feature is justified page 2-3:

The underscore is permitted within a number to break up long sequences of digits, a requirement that has long been recognized by printers.

This is an original proposal of Ichbiah and his team since there is no such explicit requirement about literals in the Steelman requirements published in 1978 by the DoD. But the feature addresses the readability needs for maintenance (requirement 1C).

The feature is included in the grammar of the first proposed Ada standard, published in July 1980 (section 2.4.1).

Use in other language

After some hours of intensive research, I have found no evidence of prior usage of this feature in any other language:

  • A (very) critical review of Ada programming language, Hidden costs in Ada by David A. Bennett, Brent D. Kornman and R. Wilson, published in May 1982, criticised this feature as an unnecessary luxury that would

    The increased visibility of a number resulting from use of this technique does not seem to be worth the cost of learning the rule or scanning for it. (...) the use of underscores in digit strings is unnecessarily inconsistent with their use in identifiers, adding gratuitous complexity to the language." (sic!)

  • other more recent extensive analysis didn't find prior usage: for example this article here mentions Ada and Perl as forerunners. But Perl appeared only in 1987 and it is not clear if that language had already the feature at that time. Another analysis by the Python designers also cite as oldest language providing this feature Ada.

  • It's not scientific evidence, but Wikipedia mentions 12 languages that now have this feature, to which you could add Fortran that accepts whitespaces as digit separators and C++14 that uses apostrophes for the same purpose.

Of course, it is not impossible that older usage existed before Ada (e.g. in less popular languages), but no evidence could be found.

  • 2
    Fantastic writeup, thank you! As Ichbiah previously designed LIS ("Language d'Implémentation de Systèmes") at the state-instituted Compagnie internationale pour l'informatique, it might well have been used there too, but after a quick look-see on the Internet, knowing more about that means going into the archives of Bull Informatique. – David Tonhofer Jan 18 at 9:21
  • C# got it within the last few years, but obviously not long enough to contend – D. Ben Knoble Jan 19 at 14:53
  • 1
    @D.BenKnoble Yes. But it's not a competition. What is very interesting here is that ADA was very in advance on its time (40 years ago), that for a very long time nobody was interested, and all of the sudden everybody wants it. It is even more interesting to see that ADA was unjustly criticised for this feature which "would not bring any benefit", when everybody knows that with separators the numbers are easier to read (so less error prone). – Christophe Jan 19 at 15:05
  • 1
    @D.BenKnoble Similarly astonishing were the debates of the C++ committee, that argued that _ could lead to ambiguity since people could put it at the beginning of a number. They therefore opted for '. Again this ambiguity was solved 40 years ago when ADA specs stated that a number cannot start with an _ and since then millions of lines of code use it without any ambiguity... There was really no need to do it differently! – Christophe Jan 19 at 15:11
  • @Christophe it's not so much that it is prone to error (I'm quite capable of writing the largest possible integer in hex correctly), but when I encounter such a number, I never know if that particular number is correct without careful counting. So it slows down reading and debugging. – gnasher729 Jan 19 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.