Is there any kind of underscore ending in numeric literals?

I just read Enhancements in Java7 ( http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html ). In this I see Underscores Numeric Literals and will try Like ....

int i=9_000;

OK.

But I see the rules for this, it also allows ...

int i=9____________________________________________________________________________________000;

      

Is there any ending Underscores

?

+3


source to share


4 answers


No, there is no limit. Java allows any number of underscores, although depending on how your compiler is implemented you might run into problems for fancy edge cases like a few billion of them :-)

Where you can have underscores, the language specification does not limit the number. I emphasize that "may" is there, because there are places where they are not allowed, for example, before the first digit, after the last, next to the decimal point, and so on. But that's another problem.

However, instead of asking if this is possible, you should ask what would be the point of more than one consecutive underscore.

A single underline makes it easier to read by naturally grouping numbers:



1_000_000
4072_1199_6645_1234
, whereas more than one tends to decrease readability:
1_0_0________000_0
4072___________________________11_9_9_6641234
+4


source


No limit. Why should it be? On the other hand, the only reason I see to use any number of underscores is to be able to do fancy things 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;

      

+5


source


Here is the definition of a decimal literal from the JLS :

DecimalNumeral:
    0
    NonZeroDigit Digitsopt
    NonZeroDigit Underscores Digits 

Digits:
    Digit
    Digit DigitsAndUnderscoresopt Digit 

Digit:
    0
    NonZeroDigit

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

DigitsAndUnderscores:
    DigitOrUnderscore
    DigitsAndUnderscores DigitOrUnderscore 

DigitOrUnderscore:
    Digit
    _

Underscores:
    _
    Underscores _

Note the recursive definition for Underscores

, fun!

+4


source


Not. Here is the doc:

http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html

In Java SE 7 and later, any number of underscores (_) can appear anywhere between digits in a numeric literal.

+2


source







All Articles