java.lang.Long 不能转换为 java.lang.Double

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32757565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:01:19  来源:igfitidea点击:

java.lang.Long cannot be cast to java.lang.Double

java

提问by Bhupendra Nath

I have a method which takes object as an input and if the input is instanceOF Long then converting the value to double value. Below is the code :

我有一个将对象作为输入的方法,如果输入是 instanceOF Long,则将值转换为双精度值。下面是代码:

public static void main(String[] args) {
    Long longInstance = new Long(15);
    Object value = longInstance;
    convertDouble(value);
}

static double convertDouble(Object longValue){
    double valueTwo = (double)longValue;
    System.out.println(valueTwo);
    return valueTwo;
}

but when I am executing the above code I am getting below exception :

但是当我执行上面的代码时,我遇到了以下异常:

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
at com.datatypes.LongTest.convertDouble(LongTest.java:12)
at com.datatypes.LongTest.main(LongTest.java:8)

Kindly let me know why its giving me exception.

请让我知道为什么它给我例外。

But if directly try to cast Long object into double then there is no Exception of classCast is coming.

但是如果直接尝试将 Long 对象转换为 double 则不会出现 classCast 异常。

Long longInstance = new Long(15);
    double valueOne = (double)longInstance;
    System.out.println(valueOne);

This is confusing.

这令人困惑。

采纳答案by Sanket Bajoria

Found explaination in JLS, https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5
Under Table 5.1. Casting conversions to primitive types

在 JLS 中找到解释,https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5
表 5.1 下。将转换转换为原始类型

    Long l = new Long(15);
    Object o = l;

When converting Object Type to primitive then it will narrowing and then unboxing.

将 Object Type 转换为原始类型时,它将缩小然后拆箱

    double d1=(double)o; 

in above statement we are trying to narrow Object to Double, but since the actual value is Longso at runtime it throws ClassCastException, as per narrowing conversion rule defined in 5.1.6. Narrowing Reference Conversion

在上面的语句中,我们试图将Object 缩小为 Double,但由于实际值是 Long所以在运行时它会抛出ClassCastException,根据5.1.6 中定义的缩小转换规则。缩小参考转换

When converting Long Type to double, it will do unboxing and then widening.

将 Long Type 转换为 double 时,会先拆箱,然后加宽

    double d2 =(double)l; 

it will first unbox the Long value by calling longvalue() method and then do the widening from long to double, which can be without error.

它将首先通过调用 longvalue() 方法对 Long 值进行拆箱,然后进行从 long 到 double 的扩展,这可以没有错误。

回答by Maas

First check if the Object is instanceof Long and then call valueOf of Long obejct

首先检查Object是否为instanceof Long,然后调用Long对象的valueOf

Snippet:

片段:

static double convertDouble(Object longValue){
        double valueTwo = -1; // whatever to state invalid!

        if(longValue instanceof Long) 
           valueTwo = ((Long) longValue).doubleValue();

        System.out.println(valueTwo);
          return valueTwo;
     }

回答by Ashwin

If you are not sure what number type the object would be then I would recommend using this code snippet:

如果您不确定对象是什么数字类型,那么我建议使用以下代码片段:

double d = 0.0;
if (obj instanceof Number) {
    d = ((Number) obj).doubleValue();
}

回答by Jochen

You try to convert Object to double, since your parameter of convertDouble is of Type object. Thus the auto-unboxing will not work. There would be two solutions: first, cast the Object to Long (checking with instanceof) second, use Long as Parameter

您尝试将 Object 转换为 double,因为您的 convertDouble 参数是 Type 对象。因此,自动拆箱将不起作用。将有两种解决方案:首先,将 Object 强制转换为 Long(使用 instanceof 检查)其次,使用 Long 作为参数

public static void main(String[] args) {
    Long longInstance = new Long(15);
    Object value = longInstance;
    convertDouble(value);
}

static double convertDouble(Long longValue){
    double valueTwo = (double)longValue;
    System.out.println(valueTwo);
    return valueTwo;
}

If you want to convert different types of arguments withing the convertDouble method, you may check with instanceof and then convert the Object to the type, you eventually have

如果你想使用 convertDouble 方法转换不同类型的参数,你可以检查 instanceof 然后将 Object 转换为类型,你最终有