简体   繁体   English

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

[英]java.lang.Long cannot be cast to java.lang.Double

I have a method which takes object as an input and if the input is instanceOF Long then converting the value to double value.我有一个将对象作为输入的方法,如果输入是 instanceOF Long,则将值转换为双精度值。 Below is the code :下面是代码:

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.这令人困惑。

Found explaination in JLS, https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5在 JLS 中找到解释, https: //docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5
Under Table 5.1.表 5.1 下。 Casting conversions to primitive types将转换转换为原始类型

    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 Long so at runtime it throws ClassCastException , as per narrowing conversion rule defined in 5.1.6.在上面的语句中,我们试图将Object 缩小为 Double ,但由于实际值是 Long所以在运行时它会根据5.1.6 中定义的缩小转换规则抛出ClassCastException Narrowing Reference Conversion缩小参考转换

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 的扩展,这可以没有错误。

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();
}

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;
     }

You try to convert Object to double, since your parameter of convertDouble is of Type object.您尝试将 Object 转换为 double,因为您的 convertDouble 参数是 Type 对象。 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 强制转换为 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 转换为类型,你最终有

This method is not very performatic but converts any Number Object to common java.lang Object (Byte, Short, Integer, Long, Float and Double).这种方法不是很高效,但可以将任何 Number 对象转换为常见的 java.lang 对象(Byte、Short、Integer、Long、Float 和 Double)。

This uses reflection to call methods of Number class (shotValue(), intValue(), etc).这使用反射来调用 Number 类的方法(shotValue()、intValue() 等)。

Unfortunatly, does not exists integerValue Method name, thus I'm using a replace string to convert integer to int.不幸的是,不存在 integerValue 方法名称,因此我使用替换字符串将整数转换为 int。

I hope was help.我希望有帮助。

public <T extends Number> T cast(Number value, Class<T> toClass) {

    try {
        String methodName = toClass.getSimpleName().toLowerCase().replace("integer", "int") + "Value";
        Method m = Number.class.getMethod(methodName);
        return (T) m.invoke(value);
    } catch (Exception ex) {
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB: class java.lang.Double cannot be cast to class java.lang.Long - MongoDB: class java.lang.Double cannot be cast to class java.lang.Long java.lang.ClassCastException:从Firebase检索数据时,不能将java.lang.Double强制转换为java.lang.Long - java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long , when retrieve data from Firebase app enigine 1.9.18,JDO 3.1.3 java.lang.ClassCastException java.lang.Long无法转换为java.lang.Double - app enigine 1.9.18, JDO 3.1.3 java.lang.ClassCastException java.lang.Long cannot be cast to java.lang.Double java.lang.Integer无法转换为java.lang.Double - java.lang.Integer cannot be cast to java.lang.Double 无法将java.lang.Long强制转换为java.lang.Integer - java.lang.Long cannot be cast to java.lang.Integer java.lang.ClassCastException:不能强制转换为java.lang.Long - java.lang.ClassCastException: cannot be cast to java.lang.Long java.lang.Integer 不能转换为 java.lang.Long - java.lang.Integer cannot be cast to java.lang.Long java.lang.Long 不能转换为 [Ljava.lang.Object; - java.lang.Long cannot be cast to [Ljava.lang.Object; java.lang.ClassCastException:java.lang.Double无法强制转换为java.lang.String - java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String java.lang.Long无法强制转换为java.math.BigInteger - java.lang.Long cannot be cast to java.math.BigInteger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM