I read that although type byte occupies 8 bits of memory, and type char occupies 16 bits of memory, the conversion from byte to char is considered narrowing because byte is signed, while char is unsigned. I think this means that when we convert a type byte with a negative value to char, we lose the negative and so that is our loss of information and so the conversion is narrowing.
I am trying to test this in a Java program.
public class Test
{
public static void main (String[] args)
{
byte num = -1;
char test = (char)num;
System.out.println(test);
}
}
I expected that printing test would output:
1
This would be -1 without the negative sign because char is unsigned. This in fact outputs:
?
Is there something wrong with my program or with my idea about converting byte to char?