You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中特殊数组大小声明的正确解读方式咨询

Understanding the Java Array Declaration: 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 of String objects.
  • book is 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 book that references a new String array capable of storing 10 million elements. Each element starts as null.

内容的提问来源于stack exchange,提问作者NovDanil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:38:45