简体   繁体   中英

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? .. i know Double to Byte for precision reasons if i am not wrong.

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 . If you need to convert a byte to a Double , you need to explicitly use something like this :

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

because only a double can be converted to a Double .

The answer is the way primitive types are auto-boxed to wrapper types in 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. 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:

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

Double with capital letters is a class, not a primitive.

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.

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:

在此处输入图片说明

So based on this table you can not directly autobox as well as can not cast to Double from byte. And as well you can not unbox to double from Byte.


But you can cast primitive to primitive type and then autobox it to its Wrapper class object. And you can unbox Wrapper class object to its primitive type and then cast it to other primitive types.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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