The minimum slot size of an operand stack and local variable table slot size is 'int'.
Then how are we getting the benefit of declaring variable as short, byte etc.
A new byte[SIZE]
is going to take 1/4th the size of a new int[SIZE]
, and when the JVM lays out a class' layout (that is, its in-memory representation, given the fields it needs to store), it can similarly compact them.
As JB Nizet points out, there's also a semantic concern. A lot of operations fundamentally work on bytes (reading binary files, reading from sockets, encryption, etc). In those cases, a type of byte
clearly indicates that you're working on a byte; otherwise, you have to either check that the high 24 bits are unset (and throw a runtime exception if they're not), or else drop them and leave users of your library wondering why 256, 512, -2816 and a whole lot of other numbers all seem to yield the same result.