File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java array fundamentals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java array fundamentals" Watch "Java array fundamentals" New topic
Author

Java array fundamentals

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi,
I have a doubt in array fundamentals.
If we give long as an array index,it will produce no compile time error.but it will throw outofMemory runtimeError(I thought this way)




I think 2147483467 is the max value of int primitive.I thought the above program would print 0.but it prints outOfMemory runtimeError,Can anyone please explain this for me.
Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You're trying to instantiate an array that can hold over 2.1 billion int values. At 16 bits each, that's nearly 4.3 GB. It's little wonder you ran out of memory.

(PS: You have the 4 and the 6 reversed. The max int value is 2147483647.)

EDIT: Of course, ints are 32 bits each. (Whoops.) See correction below.
[ March 28, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi,
Thanks for the reply.But still i don't get this.May be i am stupid.Better explain [int dimension limit] [what is long dimension],if we give long dimension in array ,what would be the result.
Thanks in advance.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The program is attempting to allocate space in memory for over 2.1 billion ints. Each int is 4 bytes long which is 32 bits. 32*2.1 billion is about 67.2 billion bits which is somewhere over 64 GB. This is far more memory that you will probably have on your system.

If you use



you can find out how much memory that your JVM can use.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
You're trying to create an array of ints. For each position in the array you will need 32 bits (not 16) to store the int because the JLS defines the exact size of an int. So, for each int in your array you'll need four bytes. 2147483647 * 4 is 8589934588. Does your system have 8589934588 bytes of memory? No, of course not, so the JVM cannot possibly create an array that big and when you try to it generates an OutOfMemory error because you are quite literally out of memory.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Keith, it's 8GB not 64GB. Regardless, they don't have the memory.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Using a long value to specify an array size results in a compiler error unless the value is explicitly cast to type int. In general, the value specifying the array size must be a type that is "int convertable" (can be widened to type int without explicit cast).

Per the Java Language Specification (section 15.10)...
The type of each dimension expression within a DimExpr must be a type that is convertible (�5.1.8) to an integral type, or a compile-time error occurs. ...this means, specifically, that the type of a dimension expression must not be long.

But not all int values are valid for specifying an array size. For example, a negative value will result in an NegativeArraySizeException.

Remember, when an object is created (and arrays are objects), the memory for that object is allocated. So if the size of the array is too large, then the OutOfMemoryError will occur.

Per the JLS (section 15.10.1)...
...space is allocated for the new array. If there is insufficient space to allocate the array, evaluation of the array creation expression completes abruptly by throwing an OutOfMemoryError.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Ken,
Thanks. I forgot to divide by 8.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ken Blair:
...For each position in the array you will need 32 bits (not 16) to store the int...

Whoops! You're right. So it's about 8.6 gigs total.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi All,
Thanks for all your effort to explain in detail.Thanks you very much.
 
wood burning stoves
 
subject: Java array fundamentals