Is it possible to index a Java array based on a byte?
i.e. something like
array[byte b] = x;
I have a very performance-critical application which reads b (in the code above) from a file, and I don’t want the overhead of converting this to an int. What is the best way to achieve this? Is there a performance-decrease as a result of using this method of indexing rather than an int?
With many thanks,
Froskoy.
There’s no overhead for “converting this to an int.” At the Java bytecode level, all
bytes are alreadyints.In any event, doing array indexing will automatically upcast to an
intanyway. None of these things will improve performance, and many will decrease performance. Just leave your code using anint.The JVM specification, section 2.11.1: