Binary literals underscore in literals
Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F. Base 2, whose digits consists of the numbers 0 and 1 you can create binary literals in Java SE 7 and later. For general-purpose programming, the decimal system is likely to be the only number system you will ever use. However, if you need to use another number system, the following example shows the correct syntax. The prefix '0x' indicates hexadecimal and '0b' indicates binary:.
In Java SE 7, the integral types byte , short , int , and long can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. The following examples show binary literals:. 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.
Instead of the relatively strict rule specified above, the use of underscores could be less limited. As seen in other languages, common rules include:. The syntax in this PEP has ultimately been selected because it covers the common use cases, and does not allow for syntax that would have to be discouraged in style guides anyway. A less common rule would be to allow underscores only every N digits where N could be 3 for decimal literals, or 4 for hexadecimal ones. This is unnecessarily restrictive, especially considering the separator placement is different in different cultures.
A proposed alternate syntax was to use whitespace for grouping. Although strings are a precedent for combining adjoining literals, the behavior can lead to unexpected effects which are not possible with underscores. Also, no other language is known to use this rule, except for languages that generally disregard any whitespace.
A preliminary patch that implements the specification given above has been posted to the issue tracker. Underscores in Numeric Literals Author: Georg Brandl, Serhiy Storchaka Status: Abstract and Rationale This PEP proposes to extend Python's syntax and number-from-string constructors so that underscores can be used as visual separators for digit grouping purposes in integral, floating-point and complex number literals.
Specification The current proposal is to allow one underscore between digits, and after base specifiers in numeric literals. Literal Grammar The production list for integer literals would therefore look like this: Constructors Following the same rules for placement, underscores will be allowed in the following constructors: Prior Art Those languages that do allow underscore grouping implement a large variety of rules for allowed placement of underscores.
Alternative Syntax Underscore Placement Rules Instead of the relatively strict rule specified above, the use of underscores could be less limited. As seen in other languages, common rules include: