Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer Octal confusion

 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
doug rosenberg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Octal constants must be composed of digits 0-7. 09 is supposed to be an octal constant but 9 is not a legal octal digit.
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Doug,
My question is:


Here value is int and 10 is obviously an int. So modulus of two ints should be int right? Then why are the cases considered as octal instead of int? Please explain.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Phal Ach:
Hi Doug,
My question is:


Here value is int and 10 is obviously an int. So modulus of two ints should be int right? Then why are the cases considered as octal instead of int? Please explain.


could not get your point ?
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Doug, Stas, thanks. Stas, you didn't get my question properly, but your answer still explained everything. Thanks.
 
Pankaj Mittal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Meena Ajay
Ranch Hand
Posts: 38
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic