hqt
hqt

Reputation: 30266

Java : large array design

When I read this link Java Criticism. At the section Large Array. There is two point I don't understand :

Java also lacks true multidimensional arrays (contiguously allocated single blocks of memory accessed by a single indirection), which limits performance for scientific and technical computing

In C, I know that multidimensional arrays is just a contiguously memory of element. So, as section above, each row in Java is an object, and multi rows just like multi objects, and they're not contiguous in memory, right ?

Java has been criticized for not supporting arrays of more than 231 - 1 (about 2.1 billion) elements ... Arrays must be indexed by int values... An attempt to access an array component with a long index value results in a compile-time error

Does this mean, if we can access an array component by long integer, the array size can be bigger ? And in that case, so, this still limit size of array is size of long, right ?

Thanks :)

Upvotes: 3

Views: 351

Answers (1)

Stephen C
Stephen C

Reputation: 718708

... each row in Java is an object, and multi rows just like multi objects, and they're not contiguous in memory, right ?

That is correct. A Java multi-dimensional array is an array of arrays, with each array represented as a separate object.

Does this mean, if we can access an array component by long integer, the array size can be bigger ?

No. The maximum size of a Java array is 2^31 - 1 elements.

The JLS says this at JLS 10.4:

"Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.

An attempt to access an array component with a long index value results in a compile-time error."


In short, the page that you linked to is correct in pointing out that Java is not ideal for applications that require the use of very large arrays. (It is possible to work around the limitations, but you can't do it cleanly without wrapping the underlying array representation as an object. And that has performance implications, and means that you can't use [...] syntax for indexing.)

Upvotes: 3

Related Questions