This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes creating array to hold long range values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating array to hold long range values" Watch "creating array to hold long range values" New topic
Author

creating array to hold long range values

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
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

Joined: Aug 16, 2005
Posts: 13869
    
  10

What do you mean with "long range"? Can you give a more detailed example of what you're trying to do (actual code)?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
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

Joined: Apr 15, 2006
Posts: 417
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

Joined: May 03, 2008
Posts: 4522
    
    5

One way is to use a 2 dimensional array.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating array to hold long range values
 
Similar Threads
Primitive casting
Casting
How to read "long" in to byte table
BigInteger to byte array of size 8 bytes (Unsigned long) in Java
BigInteger to byte array of size 8 bytes (Unsigned long) in Java