short s = 'A'; is acceptable
c = 'A';
s = c; is not, an explicit cast is required.
char c = Short.MAX_VALUE;
char c1 = Character.MAX_VALUE;
short s = (short)c;
short s1 = (short) c1;
Originally posted by John Paverd:
oops - already been answered while i was typing a reply - must type faster
Originally posted by bobby chaurasia:
Just extending the discussion a bit further:
6) a = a + 1; // fails , possible loss of precision.
case 6: How do we explain this >>>>
the compiler already knows the value of a, so adding 1 why does it require a explicit cast ?
c1 is too big for a short -- but in Java things just roll over -- if you go beyond the high value, it'll wrap down to the negative and keep going -- no overflow errors. So when you tried storing 65535 in a short -- it rolled over and ended up with a -1
Since short is signed, and the first bit of s1 is 1, the number stored in s1 must be negative. See the recent posts that explain how to determine the magnitude of a negative number, given its hex representation.