In one of the projects at my university I am working directly with Java bytecode.
After browsing the list of instructions available for the JVM (http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings) I saw that there is no such thing as [b|c|s]store,
only istore
for storing integers in a local variable. Does it mean that if in my program I write:
short a;
int b;
I am not saving any memory, because every local variable entry occupies 4 bytes?
I was always under the impression that using short
or byte
types will save some memory at runtime.
Looking at the benchmark results in @meriton's answer, it appears that using short and byte instead of int incurs a performance penalty for multiplication. Indeed, if you consider the operations in isolation, the penalty is significant.
Each can be independently sized from 0 to 65535 values, where each value is 32 bits.
class files consist of a bunch of bytecodes. Bytecode is to Java what assembler is to C++. Each bytecode is a number no larger than a byte and has a mnemonic. The numbers and their mnemonic are what you have listed in your question.
Bytecode is essentially the machine level language which runs on the Java Virtual Machine. Whenever a class is loaded, it gets a stream of bytecode per method of the class. Whenever that method is called during the execution of a program, the bytecode for that method gets invoked.
This is explained in section 2.11.1 of the JVMS:
Note that most instructions [...] do not have forms for the integral types
byte
,char
, andshort
. None have forms for theboolean
type. A compiler encodes loads of literal values of typesbyte
andshort
using Java Virtual Machine instructions that sign-extend those values to values of typeint
at compile-time or run-time. [...] Thus, most operations on values of actual typesboolean
,byte
,char
, andshort
are correctly performed by instructions operating on values of computational typeint
.
It is justified thus:
Given the Java Virtual Machine's one-byte opcode size, encoding types into opcodes places pressure on the design of its instruction set. If each typed instruction supported all of the Java Virtual Machine's run-time data types, there would be more instructions than could be represented in a byte. Instead, the instruction set of the Java Virtual Machine provides a reduced level of type support for certain operations. In other words, the instruction set is intentionally not orthogonal. Separate instructions can be used to convert between unsupported and supported data types as necessary.
However, whilst this applies to load/store of stack variables, it doesn't apply to load/store into primitive arrays; there are opcodes for all primitive types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With