JDK 16 でのIndexOutOfBoundsExceptionの実装をチェックしていたところ、long
インデックスを持つ新しいコンストラクターが導入されていることに気付きました。
/**
* Constructs a new {@code IndexOutOfBoundsException} class with an
* argument indicating the illegal index.
*
* <p>The index is included in this exception's detail message. The
* exact presentation format of the detail message is unspecified.
*
* @param index the illegal index.
* @since 16
*/
public IndexOutOfBoundsException(long index) {
super("Index out of range: " + index);
}
私の知る限り、配列インデックスは通常int
値であり、これは言語仕様セクション §10.4で確認されています。
int
配列は値でインデックス付けする必要があります。short
、byte
、またはchar
値は、単項数値昇格 (§5.6) を受けて値になるため、インデックス値としても使用できint
ます。
long
インデックス値を使用して配列コンポーネントにアクセスしようとすると、コンパイル エラーが発生します。
long
このインデックスコンストラクターがいつ(そしてなぜ)使用されるのか考えていますか?