Java 为什么我的数据类型不会自动升级为双精度

Java 为什么我的数据类型不会自动升级为双精度,java,jakarta-ee,Java,Jakarta Ee,我知道一个数据类型会自动升级为上层数据类型 字节短整型 class Temp { void check(byte x) { System.out.println(x + " is the byte type"); } void check(short x) { System.out.println(x + " is the short type"); } void check(int x) { Syste

我知道一个数据类型会自动升级为上层数据类型 字节短整型

class Temp {
    void check(byte x) {
        System.out.println(x + " is the byte type");
    }

    void check(short x) {
        System.out.println(x + " is the short type");
    }

    void check(int x) {
        System.out.println(x + " is the int type");
        int y = x;
        System.out.println(y + " is the int type");
    }

    void check(long x) {
        System.out.println(x + " is the long type");
    }

    void check(float x) {
        System.out.println(x + " is the float type");
    }

    void check(double x) {
        System.out.println(x + " is the double type");
    }

    public static void main(String args[]) {
        byte b = 42;
        char c = 'a';
        short s = 1024;
        int i = 50000;
        float f = 5.67f;
        double d = .1234;
        double result = (f * b) + (i / c) - (d * s);
        System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
        System.out.println("result =" + result);
        Temp t = new Temp();
        t.check(f * b);
        t.check(i / c);
        t.check(d * s);
        t.check(b + b);
        t.check(b * b);
        t.check(b * b * b);
        t.check(b * b * b * b * b * b * b * b * b);
        t.check(b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
                * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b);
        t.check(b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
                * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
                * b * b * b * b * b);

    }
}
输出:

238.14 + 515 - 126.3616
result =626.7784146484375
238.14 is the float type
515 is the int type
515 is the int type
126.3616 is the double type
84 is the int type
84 is the int type
1764 is the int type
1764 is the int type
74088 is the int type
74088 is the int type
-1889539584 is the int type
-1889539584 is the int type
-2147483648 is the int type
-2147483648 is the int type
0 is the int type
0 is the int type
我的问题是为什么b*b提升为int,因为42+42=84,字节范围是-128到127。 84在射程内。此外,为什么

t.check(b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
                * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b);
这条线得了0,为什么不把它提高一倍呢

我的问题是为什么b*b提升到int

因为这就是语言规范所说的

为什么[…]这一行得到了0为什么不升级到那一倍

再说一遍,因为语言不是这样定义的

阅读:

乘法运算符具有相同的优先级,并且在语法上是左结合的(它们从左向右分组)

乘法运算符的每个操作数的类型必须是可转换为基元数字类型的类型(§5.1.8),否则会发生编译时错误

对操作数执行二进制数字提升(§5.6.2)

二进制数字提升()会将
字节
操作数提升为
int
,因此在代码中的所有情况下都会使用
int*int
算法。在第一种情况下,您得到了
byte*byte
,因此两个操作数都提升为
int
;对于长行,您有一个
byte*byte
,其余的是
int*byte
,其中只有第二个操作数提升为
int
。这个选择是在编译时做出的,与执行时的值无关——JVM不会决定将值提升到
double
,因为它们超出了
int
的界限

我的问题是为什么b*b提升到int

因为这就是语言规范所说的

为什么[…]这一行得到了0为什么不升级到那一倍

再说一遍,因为语言不是这样定义的

阅读:

乘法运算符具有相同的优先级,并且在语法上是左结合的(它们从左向右分组)

乘法运算符的每个操作数的类型必须是可转换为基元数字类型的类型(§5.1.8),否则会发生编译时错误

对操作数执行二进制数字提升(§5.6.2)

二进制数字提升()会将
字节
操作数提升为
int
,因此在代码中的所有情况下都会使用
int*int
算法。在第一种情况下,您得到了
byte*byte
,因此两个操作数都提升为
int
;对于长行,您有一个
byte*byte
,其余的是
int*byte
,其中只有第二个操作数提升为
int
。这个选择是在编译时做出的,与执行时的值无关——JVM不会决定将值提升到
double
,因为它们超出了
int

“为什么b*b提升到int”的界限——事实并非如此。提升的是
b
。“为什么b*b提升到int”-不是。提升的是
b