Java中特殊数组大小声明的正确解读方式咨询
String[] book = new String[9_999_999 + 1]; Hey there! Let's break down this specific array declaration piece by piece to clear up any confusion:
1. The Variable Declaration: String[] book
This part is straightforward:
String[]defines the type: we're declaring a variable that holds an array ofStringobjects.bookis the name of our array variable.
2. The Array Initialization: new String[9_999_999 + 1]
This is where the "special" part comes in—let's split it further:
The Number Literal with Underscores: 9_999_999
Starting with Java 7, the language allows using underscores (_) in numeric literals purely for readability. They don't change the actual value of the number at all. So 9_999_999 is exactly equivalent to 9999999; the underscores just make it easier to quickly see this is 9 million (instead of counting digits manually).
The Array Length Calculation: 9_999_999 + 1
The expression inside the square brackets [] determines the length of the array. Here, we're adding 1 to 9,999,999, resulting in 10,000,000. This means the array can hold 10 million String elements.
A Quick Note on Array Default Values
Since String is a reference type in Java, every element in this newly created array will be initialized to null by default until you assign actual String objects to them.
Putting It All Together
The entire line translates to:
We're creating a variable named
bookthat references a newStringarray capable of storing 10 million elements. Each element starts asnull.
内容的提问来源于stack exchange,提问作者NovDanil

