Search code examples
javashort

Error while giving values in following way


I am getting error on following lines " Type mismatch: cannot convert from int to short"

short a;
a=09;

but not on

a=9;

why is it so.


Solution

  • Using a 0 before a number means it's an octal. You can only use it from 00 to 07, so you cannot make the conversion (it also does not work with int, since it's out of range, even if it looks like it's trying to make an implicit casting).

    09 simply is a number that does not exist, computationally speaking.