Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# creating array to hold long range values

Raj Kumar Bindal
Ranch Hand
Posts: 418
I want to create a byte array to hold long range values.Can anyone tell me how to do this.

byte byteArray[] = new byte[//here it is expecting int but i want to put long range here];

Can anyone suggest something.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
What do you mean with "long range"? Can you give a more detailed example of what you're trying to do (actual code)?

Raj Kumar Bindal
Ranch Hand
Posts: 418
if i write byte byteArray = new byte[10] , then byteArray can have maximum 10 bytes.So,like this,we can specify a maximum of 32767 , because that is maximum range of integer.
that is ,

byteArray = new byte[32767] will be valid,
but
byteArray = new byte[1000000] is not valid as we are trying to reserve space for 10 lakh bytes.

So, how can we reserve space for 10 lakh bytes.

Raj Kumar Bindal
Ranch Hand
Posts: 418
10 lakh is inside the range and is acceptable.but what if i want to give some value which is out of range.

Darryl Burke
Bartender
Posts: 5125
11
One way is to use a 2 dimensional array.

Campbell Ritchie
Sheriff
Posts: 48409
56
Originally posted by Raj Kumar Bindal:
if i write byte byteArray = new byte[10] , then byteArray can have maximum 10 bytes.So,like this,we can specify a maximum of 32767 , because that is maximum range of integer. . .

32767 might be the maximum in C but the maximum in Java is 2^31 - 1, which is 21 crore. You might do well to investigate the BigInteger class as an alternative.