简体   繁体   中英

Understanding “possible loss of precision required char found byte”

class A  {   
    public static void main(String [] varun) {
        byte b = 65;
        char ch = b;
        System.out.println(ch);
    }
}

Why its give an error:

possible loss of precision
required char
found byte

The error text is misleading.

A char is a 2 byte unsigned type (range 0 to 65535)

A byte is a 1 byte signed type (range -128 to 127).

Therefore a byte cannot be represented in a char in full generality (as you'll lose the negatives). So you get an error; albeit a misleading one.

Byte is 1 byte long, while char is 2 bytes long, so they are incompatible. You need to use casting:

class A 
{   
    public static void main(String [] varun)    
    {
        byte b = 65;
        char ch = (char) b;
        System.out.println(ch);
    }
}

Add as explicit cast as byte just take one byte and char is of two byte in java and implicit type cast does not work with byte and char.

Use

char ch = (char) b;

Your code should be like this:

char ch = (char) b;

The error it gave you it became from the fact that a byte type is an 8-bit integers and chars is 1-bit plus the bits of encoding (UTF-8, ASCII, etc).
The difference between a byte stream and a character stream is that the character stream attempts to work with characters rather than bytes. So a byte stream is 8-bit stream without encoding. That's why you have this error.

If You are assigned two different type of primitives to each other You may have two types of casting:

  • if You assign int to long , thus putting smaller type into bigger You perform widening and so called widening conversion - it is also called implicit cast

    int a = 100; long b = a;

  • on the other hand if You would perform conversion from long to int You are narrowing the type. Thus You need to perform explicit cast if You don't do that You get that possible loss of precision

    long a = 100L; int b = (int)a;

or as in Your case as @Bathsheba said "Therefore a byte cannot be represented in a char in full generality (as you'll lose the negatives). So you get an error; albeit a misleading one." - You need to explicitly cast so that You are aware of loosing data.

When one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met: • The two types are compatible. • The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place.

So the reason for it is,

For widening conversions, the numeric types, including integer and floating-point types, are compatible with each other. However, there are no automatic conversions from the numeric types to char or boolean.

The conversion from a byte to a char is a so-called Widening and Narrowing Primitive Conversion (JLS 5.1.4)

First, the byte is converted to an int via widening primitive conversion ( §5.1.2 ), and then the resulting int is converted to a char by narrowing primitive conversion ( §5.1.3 ).

The widening conversion from byte to int is fine (both are signed), but from int to char will loose both sign and (potentially) range (as char is 0 to 65536):

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.

And

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

Because of this potential loss of precision, sign and informaiton you get a compilation error and an explicit cast is necessary to signal to the compiler that you know what you're doing.

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