Reputation: 40416
I just read Enhancements in Java7( http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html). In that I see Underscores Numeric Literals and try Like....
int i=9_000;
its OK.
But I see rules for that it also allows like...
int i=9____________________________________________________________________________________000;
Is there any end of Underscores
?
Upvotes: 3
Views: 622
Reputation: 276
There's no limit. Why should there be ? On the other hand, the 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;
Upvotes: 5
Reputation: 882286
No, there's no limit. Java allows any amount of underscores although, depending on how your compiler is implemented, you may run into problems for bizarre edge cases like several billion of them :-)
In those places where you can have underscores, the language specification does not limit the quantity. I emphasise "can" there because there are places where they're not allowed, such as before the first digit, after the last, next to the decimal point and so on. But that's a different issue.
However, rather than ask if it's possible, you should instead ask what would be the point of more than one consecutive underscore.
One underscore aids readability by naturally grouping the numbers:
1_000_000 4072_1199_6645_1234whereas more than one tends to reduce readability:
1_0_0________000_0 4072___________________________11_9_9_6641234
Upvotes: 4
Reputation: 80633
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 _
Notice the recursive definition for Underscores
, fun!
Upvotes: 4
Reputation: 9801
No. Here is the document:
http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal.
Upvotes: 2