gpt4 book ai didi

java - 运行 Java,无需进行数组边界检查、强制转换检查等

转载 作者:行者123 更新时间:2023-12-02 03:38:01 26 4
gpt4 key购买 nike

我有一个 Java 程序,可以执行许多小型数组操作。我已经运行它并验证它不会产生错误,但它比预期慢。我假设这种减速很大程度上与检查数组边界等有关。

有没有办法禁用所有错误检查,以便我可以更快地运行我的程序(冒着崩溃的风险,而不是生成可理解的错误)?

最佳答案

不,这是不可能的。 Java 语言规范描述了强制转换和数组访问抛出异常的情况,但没有提及禁用此行为的机制。

ClassCastException 的抛出在 JLS Section 15.6 中描述。 :

  • A cast expression (§15.16) throws a ClassCastException if a cast is found to be impermissible at run time.

数组边界检查在JLS Section 15.10.4中描述。 :

At run time, evaluation of an array access expression behaves as follows:

  • First, the array reference expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.

  • Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.

  • Otherwise, if the value of the array reference expression is null, then a NullPointerException is thrown.

  • Otherwise, the value of the array reference expression indeed refers to an array. If the value of the index expression is less than zero, or greater than or equal to the array's length, then an ArrayIndexOutOfBoundsException is thrown.

  • Otherwise, the result of the array access is the variable of type T, within the array, selected by the value of the index expression.

关于java - 运行 Java,无需进行数组边界检查、强制转换检查等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com