Why is 1_2_3_4 a valid integer literal in java?

377 views Asked by At

Why does the following work?

int a=1_2_3_4;
System.out.println(a);   // 1234
1

There are 1 answers

3
Jon Skeet On BEST ANSWER

Numeric literals are specified in JLS 3.10.1.

A decimal numeral is either the single ASCII digit 0, representing the integer zero, or consists of an ASCII digit from 1 to 9 optionally followed by one or more ASCII digits from 0 to 9 interspersed with underscores, representing a positive integer.

[...]

A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits interspersed with underscores, and can represent a positive, zero, or negative integer.

[...]

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

[...]

A binary numeral consists of the leading ASCII characters 0b or 0B followed by one or more of the ASCII digits 0 or 1 interspersed with underscores, and can represent a positive, zero, or negative integer.

If you're asking why the underscores don't have to be in groups of three digits for decimal literals, different cultures group numbers differently - and certainly for hex and binary literals, depending on the usage, you could want all kinds of different obvious groupings.