why it must be casted from char to short? short x='a'; //it will compile with error.it need cast short x=(short)'a';//it compiles correct. char's range is 0-255,but short is -256-255.so I think the statement short x='a'; is correct. Who can tell me the reason? Thanks.
Hi rainbow, It's is a 32 bit world these days. Integral types and ranges: For byte, from -128 to 127, inclusive For short, from -32768 to 32767, inclusive For int, from -2147483648 to 2147483647, inclusive For long, from -9223372036854775808 to 9223372036854775807, inclusive For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535 Regds Sathish
Therefor, all chars may not be short and all short may not be chars. So, u need an explicit cast. For eg, u can represent -100 in a short but u can't represent the same -100 in a char coz it takes value from 0 to 2^16-1. hope this helps :-) -sampaths77