Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java buffer indexed by long instead of int?

Tags:

java

It looks like all java containers, buffers, arrays and etc, can only be indexed by int. On C++, I can index by unsidned long for example.

What is the solution for this in Java? I can surely create my own class that uses lots of int32 indexable buffers and access the rigth one, but is there a better and simpler way?

like image 977
Guerlando OCs Avatar asked Oct 15 '22 20:10

Guerlando OCs


1 Answers

According to the Java language specification.

10.4 Array Access

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) and become int values.

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

And from my own perspective, since the Array.length returns an int, there would be no need to have an index beyond Integer.MAX_VALUE. So indexing via a long wouldn't be necessary.

like image 58
WJS Avatar answered Oct 18 '22 09:10

WJS