Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type of Array's index?

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside and in case you are interested, you can also read this thread: Why does array index have to be an integer?
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic