My question is:
If I got it right from the Java disassembly, when I use
byte a=3,b=5;
System.out.println(a+b);
would actually use int instead of byte. Also all local memory slots are 4B just as stack slots. I realize that allocating a byte array would probably act more efficiently, but is it true that using a single byte value is ultimately inefficient? (The same point for short)
The first rule of performance tuning should be to write simple, clear code.
In this example, there is no performance difference and even if there were the println() takes 10,000x times longer making any difference notional.
How a program appears in byte-code and how it appears in native code is different.
Not all slots are 4-bytes in the JVM. e.g. a reference on a 64-bit machine can be 8-bytes but it still uses one “slot”
Your machine doesn’t have slots. It does have registers which are typically 32-bit or 64-bit.
In your example, byte operations are used, which are just as efficient as int operations, and can produce a different result so they are still required.
Note: an object with
byteorshortfields can be smaller than one with aintfields.In this example, the JVM can calculate
conce so it doesn’t needaorb