2

For example, I get error "Cannot convert from long to int" while trying following

long size = 32;
int a[] = new int[size];

Java restrict array size to int, which limits me to create max ~2 billion elements.

Why do we have this limit?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Deep
  • 5,772
  • 2
  • 26
  • 36
  • 1
    This is a restriction of the JLS (there is a slight restriction on the implementation). The JLS simply doesn't allow array with a non-int (i.e. long) size. – user2864740 Jun 25 '14 at 04:36
  • My question is why that limit? – Deep Jun 25 '14 at 04:37
  • Actually, you are limited to arrays with a couple billion elements, but if each element is larger than a byte, it'll take **much** more memory than 2GB. – azurefrog Jun 25 '14 at 04:38
  • @Deep One would have to ask the JLS designers (.NET does allow such long indexes); presumably such a use-case "wasn't warranted/needed" at the time (Java was conceptualized in what, 1990?). And either nobody working on the JLS cares enough about it now or considers it too little of a benefit/work ratio to add in - I'm finally glad that Java 8 made Java less dinosauric, can't get all our wishes :| – user2864740 Jun 25 '14 at 04:38
  • 3
    Clearly not a duplicate. This question is "why do we have this limit", and the other question doesn't address that at all. – Dawood ibn Kareem Jun 25 '14 at 04:39
  • 1
    @DavidWallace I agree that it's not a duplicate, so I've voted to reopen (for that reason), but .. I don't think it's a "good" question either as the result is "because the JLS says so". – user2864740 Jun 25 '14 at 04:40
  • 3
    No, the correct answer is "nobody knows, except for the designers of Java". Any answer that just quotes the JLS is not addressing the "why". – Dawood ibn Kareem Jun 25 '14 at 04:49
  • There is design work to possibly expand this limit, among other array changes -- I recall a presentation from one of the JVM Language Summits named Arrays 2.0 or something like that. That presentation or related work may provide insight into "why". – Jeffrey Bosboom Jun 25 '14 at 04:52
  • 1
    @DavidWallace In Java, Arrays are objects. They have an int length. Why is it an int? Because the designers of Java made it one. They documented that design decision. Regardless, presumably you should be using a (sliding) [`ByteBuffer`](http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html) and it still has an `int` capacity. – Elliott Frisch Jun 25 '14 at 05:32

2 Answers2

1

Every array has a length, and it's specified in the JLS-10.7 that it is an int.

The public final field length, which contains the number of components of the array. length may be positive or zero.

public final int length = X ;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

According to the Java language specification 15.10

the dimension expression in an array creation expression must be convertible to type int at compile time. The type long is not convertible to int at compile time, and thus causes a compile error.

paisanco
  • 4,098
  • 6
  • 27
  • 33