gpt4 book ai didi

java - javac如何计算public static final int NaN = 0/0?

转载 作者:行者123 更新时间:2023-12-02 00:05:51 25 4
gpt4 key购买 nike

我想知道javac如何计算这个常量表达式?

class Test {
public static final int NaN = 0 / 0;
...

最佳答案

它在 Java Language Specification, §15.17.2 中定义:

There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is -1, then integer overflow occurs and the result is equal to the dividend. Despite the overflow, no exception is thrown in this case. On the other hand, if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.

这意味着什么:如果被除数为 0,那么除数是多少并不重要;它会直接抛出ArithmeticException

现在,如果您使用 double ,那么JLS §15.17.2将返回NaN .

关于java - javac如何计算public static final int NaN = 0/0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899322/

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