aspose file tools*
The moose likes Beginning Java and the fly likes Why does array index have to be an integer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does array index have to be an integer?" Watch "Why does array index have to be an integer?" New topic
Author

Why does array index have to be an integer?

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

According to the Java language specification :[Reference: Arrays: 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.1) and become int values. An attempt to access an array component with a long index value results in a compile-time error.


Why does array index have to be an integer and integer only?

I guess it has got something to do with ease of adressing(?)..but I want a more reliable and exact answer.

Please help.

Thanks.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, it has to be a countable set of symbols, because the index refers to the individual discrete cells of an array, ordered from one end to the other. If floating point numbers were allowed, how would you handle the fractional part?

[Jess in Action][AskingGoodQuestions]
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Why int and not long.

The designers probably thought that an Array will never exceed 2,147,483,647 elements.


JDBCSupport - An easy to use, light-weight JDBC framework -
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Ernest Friedman-Hill wrote:Well, it has to be a countable set of symbols, because the index refers to the individual discrete cells of an array, ordered from one end to the other. If floating point numbers were allowed, how would you handle the fractional part?


Thanks for you reply EFH..

I understand what you say; but then there are associative arrays which use arbitrary indices and are not directly supported by Java.

My question is more like - why did the designers prefer only integers? Does using integers help, in any way, with providing better/faster access(calculating the next addressable location etc.) or Is it just part of the spec?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I suspect using ints dates back to the time of C when you found the addressable location by adding the index to the location of the array. It is awkward to find the next member by adding a floating-point number, but very fast by adding an integer. You can do it at the chip level with relative addressing.
I presume they chose int rather than long because they thought nobody would need an array of ≥ 2^31 elements. Or even to prohibit anybody using an array with 2^31 elements.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Campbell Ritchie wrote:I suspect using ints dates back to the time of C when you found the addressable location by adding the index to the location of the array. It is awkward to find the next member by adding a floating-point number, but very fast by adding an integer. You can do it at the chip level with relative addressing.
I presume they chose int rather than long because they thought nobody would need an array of ≥ 2^31 elements. Or even to prohibit anybody using an array with 2^31 elements.


Many thanks CR..

[I could be totally wrong here : ]
From what I remember,in C, the name of the array refers(or points) to the location in memory from where the array starts. The indexes are used to increment the pointer variable and thereby, access the memory location where the respective array elelments are stored. For e.g: a[1],a[2] are *(a++) and *((a++)++) respectively. For a 32- bit platform, the pointer variable is of 4 bytes, so having a integer(4 bytes also) as index probably helps?

Please correct me if I am wrong...

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
As far as I can tell, your C code is correct.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Sebastian Janisch wrote:Why int and not long.

The designers probably thought that an Array will never exceed 2,147,483,647 elements.

I just tried writing a class called JumboArray that used long for indexing. It used a three dimensional array:
- the top level dimension had a size of length() / Integer.MAX_VALUE / Integer.MAX_VALUE, rounding up to 1 if it was 0.
- the dimension below that had a size of (length() / Integer.MAX_VALUE) % Integer.MAX_VALUE, rounding up to 1 if it was 0.
- the dimension below that had up to Integer.MAX_VALUE elements

This jumbo array could be indexed as follows:
I completed it, then tried testing with Long.MAX_VALUE as the size: java.lang.OutOfMemoryError. Specifying Integer.MAX_VALUE as the size: java.lang.OutOfMemoryError. I tried setting the -Xmx flag to 1024MB - still java.lang.OutOfMemoryError. Whatever I chose, java.lang.OutOfMemoryError. (Also caused by a limitation of the flag.) I had to go down to Integer.MAX_VALUE >> 8 as the size before it accepted it.

That makes sense of course, since Integer.MAX_VAlUE already is 2GB - if the elements are bytes. If the elements are int that's already 8GB. For Object, that's (probably) 8GB plus the size of the objects. And that was excluding the overhead for the array(s).

So yeah, Integer.MAX_VALUE seems like a reasonable upper bound Be glad they didn't choose short or char...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


An "associative array" like in Perl or Ruby is not an array at all, in the older comp-sci sense. An array is, by definition, a linear section of memory addressed in order. Arrays are far older than C, of course: FORTRAN, one of the very first compiled languages, certainly had them. In Java an associative array is called a Map, and although there's no direct language support for them, there are many Map types in the API.

Pascal arrays could use an enumeration as an index, so you could have an array whose indices were the seven colors of the rainbow, for instance. Underneath, the seven names would be compiled down to the first seven integers, but the compiler type-checked so you could only use those seven values in code.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Simplicity is probably the most correct answer.

Regardless, if you have a program that has several billion items to store, an array of any type is the wrong data structure to be using. There is a reason disk based data structures were invented. Of course this gets into how databases are actually implemented and likely further than you need to go.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
 
subject: Why does array index have to be an integer?