posted 6 years ago

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 ?

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 ?

posted 6 years ago

- 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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Mike Simmons

Ranch Hand

Posts: 3090

14

Campbell Ritchie

Marshal

Posts: 56598

172

posted 6 years ago

If you want the number to be a

8 is 2^3, 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.Consider Paul's rocket mass heater. |