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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
charis unsigned,shortis signed.So while they are both 2-byte long, they use the sixteenth bit for different purposes.
The range of the
chartype is 0 to 2^16 – 1 (0 to 65535).The
shortrange is -2^15 to 2^15 – 1 (−32,768 to 32,767).