0

I stumbled across this seemingly odd statement in the JLS, section 3.10.1 (integer literals) today:

Underscores are allowed as separators between digits that denote the integer.

Lo and behold the following is completely valid (example):

int x = 1_2_3_4_5____6;

Underscores may only appear between digits, leading/trailing underscores are invalid.

What was the rationale for this? Is it some holdover from some other language? Some style that was common at the time? It seems like a rather strange thing to allow.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 2
    What is more readable to you: `5236323` or `5_236_323`? – Flown Mar 02 '17 at 19:19
  • Nevermind. http://stackoverflow.com/questions/18175809/wierd-syntax-underscores-between-digits?rq=1. – Jason C Mar 02 '17 at 19:19
  • @Flown Yeah. The dupe says readability too. Deleting this. Or not, now it's answered. (Also huh, there's a whole chain of dupes here that didn't come up in the auto-search. Fun!) – Jason C Mar 02 '17 at 19:20
  • 2
    *Is it some holdover from some other language?* It was added in Java 7, so no. – shmosel Mar 02 '17 at 19:25

1 Answers1

3

The idea behind this is to be able to display a big number that is easily readable.

Example: int x = 1_000_000;

There are other use cases for this but this is the most used i think.

See the documentation for more information.