• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java array fundamentals

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith, it's 8GB not 64GB. Regardless, they don't have the memory.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken,
Thanks. I forgot to divide by 8.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for all your effort to explain in detail.Thanks you very much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic