Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding "possible loss of precision required char found byte"

Tags:

java

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

like image 866
Varun Kumar Avatar asked Dec 26 '22 10:12

Varun Kumar


1 Answers

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.

like image 144
Bathsheba Avatar answered May 04 '23 00:05

Bathsheba