简体   繁体   English

为什么不能将 Byte 对象/字节值转换为 Double 对象? 从 Byte 到 Double 的转换会影响精度吗?

[英]Why can not cast a Byte object/byte value to Double object? does conversion from Byte to Double affect precision?

public class Primitive {
    public static void main(String []args) {


    byte x=5;
    Double y=(Double)x;  //Error :  Cannot cast from byte to Double.

    Byte n=7;
    Double m=(Double)n; //Error : cannot cast from Byte to Double.

    double c=n; //working right ..."double is primitive and Byte is object ".
    }
}

What is the point from preventing casting Byte to Double?阻止将 Byte 转换为 Double 有什么意义? .. i know Double to Byte for precision reasons if i am not wrong. .. 如果我没有错的话,出于精确原因,我知道 Double to Byte。

Because that's how auto-boxing and un-boxing works.因为这就是自动装箱取消装箱的工作原理。

This code works fine :此代码工作正常:

    byte x = 5;
    Integer i = (int) x;

Reason : boxing conversion map primitives and their wrappers directly .原因:直接对转换映射原语及其包装器进行装箱。 What I am saying is only a byte can be converted to a Byte without explicit type-casting .我要说的是只有一个字节可以转换为一个 Byte 无需显式类型转换 If you need to convert a byte to a Double , you need to explicitly use something like this :如果需要将byte转换为Double ,则需要显式使用以下内容:

    byte x = 5;
    Double d = (Double) (double) x; 

because only a double can be converted to a Double .因为只有double可以转换为Double

The answer is the way primitive types are auto-boxed to wrapper types in Java.答案是原始类型在 Java 中自动装箱为包装类型的方式。 You can also do something like你也可以做类似的事情

Double y = Byte.valueOf(x).doubleValue();
Double z = (double) x;

You were trying to cast a primitive byte to the Double class, which you cannot do.您试图将原始byte转换为Double类,但您不能这样做。 If you try the following code instead, you won't have a problem:如果您改为尝试以下代码,则不会有问题:

byte x=5;
double y = (double)x;  // No error

If you want to use the Double class instead of primitive double , then you can try this:如果你想使用Double类而不是原始double ,那么你可以试试这个:

byte x=5;
Double y = Double.valueOf(x).doubleValue();

Double with capital letters is a class, not a primitive.带大写字母的 Double 是一个类,而不是一个原始类型。

What you want to do is你想做的是

 byte x=5;
 double y=(double)x;  

 byte n=7;
 double m=(double)n; 

 double c=n; 

Casting a double into a byte with cause loss of precision (you'll loose all decimals).将双精度转换为字节会导致精度损失(您将丢失所有小数)。 A byte is like an int but 8-bit long.一个字节就像一个 int 但有 8 位长。

You can have look at this documentation of autoboxing and unboxing.您可以查看此自动装箱和拆箱文档

Autoboxing and unboxing lets developers write cleaner code, making it easier to read.自动装箱和拆箱使开发人员能够编写更简洁的代码,使其更易于阅读。 The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:下表列出了 Java 编译器用于自动装箱和拆箱的基本类型及其相应的包装类:

在此处输入图片说明

So based on this table you can not directly autobox as well as can not cast to Double from byte.因此,基于此表,您不能直接自动装箱,也不能从字节转换为 Double。 And as well you can not unbox to double from Byte.同样,您无法从 Byte 拆箱以加倍。


But you can cast primitive to primitive type and then autobox it to its Wrapper class object.但是您可以将原始类型转换为原始类型,然后将其自动装箱到其 Wrapper 类对象。 And you can unbox Wrapper class object to its primitive type and then cast it to other primitive types.您可以将 Wrapper 类对象拆箱为其原始类型,然后将其转换为其他原始类型。

byte x = 5;
double temp = x;
Double y = temp;

// Can be written as
byte x1 = 5;
Double y1 = (double) x1;

// ================
Byte n = 7;
byte byteVal = n;
double doubleVal = byteVal;
Double m = doubleVal;

// Can be written as
Byte n1 = 7;
Double m1 = (double) (byte) n1;

// =================
// But I still wonder how below code is working
double c = n;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM