0

Recently I have found an array declaration in Java, as for me it was some kind specific.

String[] book = new String[9_999_999 + 1];

Could someone please clarify how to read this declaration correctly.

2
  • 1
    What is confusing? The underscores? Commented May 25, 2018 at 6:57
  • 1
    can be simplified to new String[10000000] Commented May 25, 2018 at 6:58

2 Answers 2

4

JLS says:

3.10.1. Integer Literals

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

Java Tutorial says:

Primitive Data types

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. 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.

The following example shows other ways you can use the underscore in numeric literals:

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =  3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
Sign up to request clarification or add additional context in comments.

Comments

0

Underscore characters are used to separate the groups of digits. It is used to add readability to the code. So , 9_999_99 is nothing but 9999999. The total will be 10000000 sized array(9999999 + 1).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.