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.

NovDanil
  • 51
  • 8

2 Answers2

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;
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
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).

Nirmal
  • 549
  • 1
  • 9
  • 24