gpt4 book ai didi

Java 的类型是什么(long a - double b)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:40:08 25 4
gpt4 key购买 nike

如题中所问:从 long 值中减去 double 值时内部会发生什么?结果是 double 还是 long 类型,这种行为是否甚至在 Java 规范中指定,或者这种行为是特定于 jvm 的?

所以我知道我可以测试自己的 jvm 功能:

    public class Test {

/**
* @param args
*/
public static void main(String[] args) {
double b = 1000;
long a = 1000;
Object c = a - b;
Class cls = c.getClass();
System.out.println("The type of the object is: " + cls.getName());
}

}

输出:

    The type of the object is: java.lang.Double

但这在某处指定了吗?或者它是特定于我使用的 jvm?

最佳答案

这是规范的适用部分:

4.2.4。浮点运算

If at least one of the operands to a numerical operator is of type double, then the operation is carried out using 64-bit floating-point arithmetic, and the result of the numerical operator is a value of type double. If the other operand is not a double, it is first widened (§5.1.5) to type double by numeric promotion (§5.6).

http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.4

关于Java 的类型是什么(long a - double b),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16787947/

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