Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer Octal confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer Octal confusion" Watch "Integer Octal confusion" New topic
Author

Integer Octal confusion

Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
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

Joined: Oct 31, 2007
Posts: 29
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.


SJCP 6.0
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
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.
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

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 ?


Good luck for yourself.
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Oh Doug, Stas, thanks. Stas, you didn't get my question properly, but your answer still explained everything. Thanks.
Pankaj Mittal
Ranch Hand

Joined: Jul 09, 2008
Posts: 32
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


2008 SCJP(5.0) 88%
Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

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 ]

Cheers, Meena
OCPJP 6
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer Octal confusion
 
Similar Threads
Dan's question:Topic. Literals
K & B Doubt: Page 177 of SCJP 5.0?
Strings Immutable ??