Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Maximum number of elements in an array ?

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 26613
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 6977
164
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic