Why double in Java for a long time?

SSCCE:

public class Test {

    public static void main(String[] args) {
        Long a = new Long(1L);
        new A(a);
    }

    static class A {
        A(int i) {
            System.out.println("int");
        }

        A(double d) {
            System.out.println("double");
        }
    }
}

Output:

double

There will be no compilation errors when printing. It can work normally and call the two parameter constructor But why?

Solution

Converting long to int is narrowing primitive conversion because it may lose the overall size of the value Convert the length to twice as long as to widening primitive conversion

The compiler will automatically generate assignment context conversion for parameters This includes expanding the original transformation, but not shrinking the original transformation Because a method with an int parameter needs to narrow the conversion, it is not suitable for this call

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>