aspose file tools*
The moose likes Java in General and the fly likes Type of Array's index? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Type of Array Watch "Type of Array New topic
Author

Type of Array's index?

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
What is the type of an array's index? Normally I have used the primitive int out of force of habit. But I know that byte will work. The point is, I seem to recall reading that if the index is a byte or short, that there will be a type conversion (upcasting?) to int done behind the scenes.

If anyone knows where in the API this is documented, I'd like to know.

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#25570
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sorry, meant:

http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#25570
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679


Right, that settles it. Thanks for the link.

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 (§) and become int values. An attempt to access an array component with a long index value results in a compile-time error.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679


One thing this page wasn't clear on is as follows...

ok we have established that when a byte variable is used as an array index, there is numerical promotion to an integer. But as far as I can see, the variable is still a byte. For example, if we have byte b = 0; then later we have array[b] = something; then after that b is still a byte primitive.

also, any kind of arithmetic operation does the same kind of upcasting for example...

for( byte b = 0; b<64; b++ ) {...}

then there is numerical promotion for the purpose if doing the increment, but within the body of the loop, b is still a byte, and we can treat it is such without having to deal with loss of precision issues.

Do I have this right?

Thanks a lot.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Fred Hamilton wrote:
then there is numerical promotion for the purpose if doing the increment, but within the body of the loop, b is still a byte, and we can treat it is such without having to deal with loss of precision issues.

Do I have this right?

How would a test look like?

And beware of the inifinite loop:


http://home.arcor.de/hirnstrom/bewerbung
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

That's not an infinite loop since char can go all the way up to 65535. If you replace char with byte then yes, it will never stop.

Using the upper bounds for a type can also lead to problems:
When i becomes Integer.MAX_VALUE, the condition will hold and the body is executed. Then i is increased, wraps around and becomes Integer.MIN_VALUE. The condition will still hold until i becomes Integer.MAX_VALUE and Integer.MIN_VALUE after that again.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

As an aside and in case you are interested, you can also read this thread: Why does array index have to be an integer?


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Thanks for the link, yeah that helps. Anyways, I'll just put the idea to the test. The program design is such that there is no chance of any kind of an infinite loop, unless I do a typo, and have a stop condition of 644 instead of 64.

The prime motivation is to experiment with an alternate way of doing a multikey bubble sort. Each row of an array has 4 integers And all four ints can be represented by bytes without loss of information. And the idea is to see if any performance benefits accrue from using bit shifts and masks to combine the 4 numbers into one 32-bit int, and thus should be able to make it a single key sort.

But before I did the research and work necessary to implement that in my program, I just started this thread cause I wanted to nail down a few things about how numbers are actually represented.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
You are reminded that 4 of the 5 Java™ integer types are two's complement type, in 8 16 32 and 64 bits.
The 5 type is char which is maintained as a 16 bit unsigned integer.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:You are reminded that 4 of the 5 Java™ integer types are two's complement type, in 8 16 32 and 64 bits.
The 5 type is char which is maintained as a 16 bit unsigned integer.


ok noted thanks, except as I see it all of my data are integral numbers ranging from -100 to 100, so I shouldn't need to concern myself with that.

regards.
 
 
subject: Type of Array's index?