programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How many digits an octal number have ?

ashwin bhawsar
Ranch Hand
Posts: 62
As per the topic http://www.coderanch.com/t/452169/java/java/Literals#2013737
an Octal number can have up-to 21 digits , but i tried out this code snippet :

It gives following compilation error :

Example.java:9: integer number too large: 0111111111111111111111
long a=0111111111111111111111;

Can anyone explain me how do we calculate how many digits a number can hold ?
And is there any relation between the range(max value) and length(max no. of digits) of a number ?

Rob Spoor
Sheriff
Posts: 21135
87
• 1
The number of digits isn't relevant, it's the value. The value of any int can be between -(2^31) and 2^31 - 1. It doesn't matter if you represent these values as decimal, hexadecimal or octal, those are the bounds.

Wouter Oet
Bartender
Posts: 2700
• 1
The thing is that the octal number in the code is actually an int. What you want is:

Mike Simmons
Ranch Hand
Posts: 3090
14
• 1
Or possibly
but then your co-workers may be forced to kill you.

Campbell Ritchie
Marshal
Posts: 56598
172
If you want the number to be a long, it must be followed by an L. [Avoid l even though the compiler will take it happily.]

8 is 2^3, a long goes up to 2^63 - 1, so you divide 3 into 63 and add one for the remainder and you can work it out. Or you can look in the Java™ Language Specification.

ashwin bhawsar
Ranch Hand
Posts: 62
Thanks a lot for your replies.

@Rob Spoor : I'll give a thought on "The number of digits isn't relevant, it's the value" and will get back soon with more queries

 Consider Paul's rocket mass heater.