java - 为什么 IndexOutOfBoundsException 现在在 Java 16 中有一个带有长索引作为参数的构造函数?

标签 java arrays indexoutofboundsexception long-integer java-16

我正在检查 IndexOutOfBoundsException 的执行情况在 JDK 16 中,我注意到一个带有 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值,这在 Language Specification section §10.4 中得到证实。 :

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6) and become int values.


An attempt to access an array component with a long index value results in a compile-time error.


知道什么时候(以及为什么)这个 long将使用索引构造函数?

最佳答案

引用评论以供将来引用:

This was precipitated by Project Panama, which brings better native heap access to Java. The Foreign Memory API (a replacement for direct byte buffers) allows long-indexed heap access to native memory segments, motivating this change to IOOBE. – Brian Goetz


TL;博士 它与以下功能增强(JDK-8255150)有关:Add utility methods to check long indexes and ranges

Description
This is related to JDK-8135248. The goal is to add a similar set of methods but rather than operate on int arguments, the new methods operate on long arguments.

The new methods in Objects are:

public static long checkIndex(long index, long length) public static long checkFromToIndex(long fromIndex, long toIndex, long length) public static long checkFromIndexSize(long fromIndex, long size, long length)

They mirror the int utility methods.

As is the case with the int checkIndex(), the long checkIndex() method will be JIT compiled as an intrinsic. That allows the JIT to compile checkIndex to an unsigned comparison and properly recognize it as range check that then becomes a candidate for the existing range check optimizations. This has proven to be important for panama's MemorySegment and a prototype of this change (with some extra c2 improvements) showed that panama micro benchmark results improve significantly.


来自关于该主题的另一个来源:JDK 16: Checking Indexes and Ranges of Longs :

In my last post, I described the day period support added with JDK 16 Early Access Build 25. That same build also added methods for checking indexes and ranges of long values, which is the subject of this post. JDK-8255150 (“Add utility methods to check long indexes and ranges”) is the Enhancement used to add utility methods for checking long indexes and ranges similar to what JDK-8135248 (“Add utility methods to check indexes and ranges”) added for integers with JDK 9. JDK-8255150 states, “The goal is to add a similar set of methods [as JDK-8135248] but rather than operate on int arguments, the new methods operate on long arguments.”

The greatest beneficiary of these newly added long-supporting methods may be the authors, maintainers, and users of the foreign memory access API as described in this mailing list message: “We have to jump through quite a few hoops in the implementation of the foreign memory access API in order to leverage the intrinsification of int-based index checks, and even then we are not covering the cases where the numbers are larger than ints. Looking forward to being able to remove those hacks!”

关于java - 为什么 IndexOutOfBoundsException 现在在 Java 16 中有一个带有长索引作为参数的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66673634/

相关文章:

java - 在 Tomcat 中更改 Apache 文件上传的临时目录

java - 在 Android 上从另一个 xml 文件中的 DatePicker 获取日期

arrays - 多维可变向量 : possible? 如果是这样,怎么办?

ios - 我们如何在ios中将DateAndTime设置为升序

java - 访问 arrayList 中另一个字符串之前和之后的字符串

java - 如何在Java中为MVC模型实现undo/redo?

java - 使用 selenium webdriver 找不到下拉列表元素

php - 如何使用 foreach 循环在 PHP 和 HTML 数组中回显名字、姓氏和电子邮件地址对象

java - Android 获取 IndexOutOfBundException

java - 在java中解析CSV文件,并处理空值