Why does Java convert byte and short operands to ints during numeric promotion

StackOverflow https://stackoverflow.com/questions/4575637

  •  14-10-2019
  •  | 
  •  

Question

What are the reasons behind the extension of small datatypes (e.g. byte) to int during the Numeric Promotion process? Wouldn't it be possible to perform most of the operations directly on these smaller datatypes?

Was it helpful?

Solution

The VM is a virtual machine, the JIT translates this to native code and optimises it then. All registers are either 32-bit or 64-bit and "shorter" operations are not necessarily and faster.

OTHER TIPS

The VM does not support integers smaller than 4 octets, so when running in the VM you are operating on 4 octet ints or 8 octet longs. (Well it does for arrays, barray sarray, etc, just not for single values).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top