开发者

short and char type in Java [duplicate]

开发者 https://www.devze.com 2023-02-12 19:22 出处:网络
This question already has answers here: 开发者_开发百科What is the difference between Short and Character apart from processing?
This question already has answers here: 开发者_开发百科 What is the difference between Short and Character apart from processing? (3 answers) Closed 3 years ago.

According to the Java standard the short and char types both use 2 bytes so when I code something like:

char ch = 'c';
short s = ch;

There is an error saying "possible loss of precision". What am I missing here?


char is unsigned, short is signed.

So while they are both 2-byte long, they use the sixteenth bit for different purposes.

The range of the char type is 0 to 2^16 - 1 (0 to 65535).

The short range is -2^15 to 2^15 - 1 (−32,768 to 32,767).


The difference is that char is unsigned, short is signed. Thus, half the range of values of char is too big to be represented as a short (and of course, in symmetry, char cannot represent any of the negative values short can).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号