Hi there. Below is the code sample from javablackbelt. There is a compiler error in the code at Line 12. Explanation for this is Octal digits can have range from 0 to 7, so the value 09 is wrong. But as far as I understood, result of modulus operation on two ints is always int. Can anybody please tell me that where did this octal come from? Code is as below:
When you put Ox in front of a integer that is sign of hexadecimal similarly when you will put 0 in front of a digit it shows that base of that integer is octal. so remove 0 in front of that code, it'll run
Phal, I think here you are trying to differentiate between octal and int taking them as separate datatypes, when you say "Then why are the cases considered as octal instead of int?"
Decimal, Octal and Hexadecimal are just different representations using which one can write integer literals.ex:base 10 - decimal,8 - octal and 16 - hexadecimal
The values in the case statements are considered as octal representation of int, as they are prefixed with 0, instead of the normal decimal representation of int which would be without prefixing 0.
So adding to what pankaj said, when value is 09 ,it is an octal representation of an integer number but not a valid one.Because, an octal integer literal begins with the digit 0 and contains any of the digits 0 through 7.That is why you must have got the compiler error.
Hope this helps. [ July 14, 2008: Message edited by: Meena Subramanian ]
Joined: May 09, 2008
Thanks Pankaj and Meena. I had already tried to remove 0 and it worked. Meena your detailed discussion on that was really nice. Thanks to all of you.