• Post Reply Bookmark Topic Watch Topic
  • New Topic

Maximum number of elements in an array ?  RSS feed

 
Raghavendra Shockley
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the max we can put in an array? It seems that it is limited to an int, because putting a long index caused a compiler error.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's correct. (Although you might not have enough memory to create an array with Integer.MAX_VALUE elements.)
 
Raghavendra Shockley
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, that's correct. (Although you might not have enough memory to create an array with Integer.MAX_VALUE elements.)


Even though I am unable to see why someone would want a "huge" array, I still wonder why did they put this limit ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Shockley wrote:
Paul Clapham wrote:Yes, that's correct. (Although you might not have enough memory to create an array with Integer.MAX_VALUE elements.)


Even though I am unable to see why someone would want a "huge" array, I still wonder why did they put this limit ?


A larger index would be wasteful and there would be very little benefit to doing it. A 4-byte int has been the most common word size on computer hardware for many years now, and will continue to be until 64-bit machines become more popular in the next few years.
 
Tim Moores
Saloon Keeper
Posts: 4019
94
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The smallest possible thing you cold store in an array is a byte - which takes up (rather obviously) at least one byte to store. Using a long for the array index type would mean you'd have at least 2^31 bytes - so that's 2 GB just for that one array. But around the time the JLS (Java Language Specification) was written it was more typical for a desktop computer to have something like 4 MB of RAM (not that Java was originally written to run on desktop computers, mind you). So there was no point in enabling something that you could not possibly make use of.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!