简体   繁体   中英

How does bipush work in JVM?

I understand iload takes in integers -1 to 5, but how can you extend to higher numbers using a bipush instruction? How is the specific integer being stored with the bytecode?

I think you're looking for section 2.11 of the JVMS , which deals with instruction representation. In particular, it uses the obvious order: the opcode, immediately followed by operands in order, big-endian (as all Java representations). In the case of bipush , this would be the byte 0x10 followed by the literal value.

There's several different instructions that can be used to push an integer constant.

The smallest is the iconst_* instructions. These are only a single byte, because the value is encoded in the opcode itself. iconst_1, iconst_2, etc. are different opcodes. iconst_5 for example would be encoded as the byte 08 .

Note: iload is a completely unrelated instruction used for loading the value of a local variable. You must have been thinking of iconst_*.

Next is bipush , which can push a constant between -128 and 127. This instruction is two bytes long - the first byte is the opcode, and the second byte is a signed 8 bit integer. You can even use it to push constants in the range -1 to 5, although doing so would take up more space in the classfile than necessary. For example, bipush 5 would be encoded as 10 05 . (0x10 is the opcode for bipush)

Next is sipush , which is the same except that it stores a 16 bit constant instead of an 8 bit constant, and hence the instruction is three bytes long. The opcode for sipush is 0x11, so sipush 5 would be encoded as the three byte sequence 11 00 05 .

You might wonder how integer constants which don't fit in 16 bits are stored. In this case, the compiler creates entries in a separate section of the classfile called the constant pool, and then uses the ldc or ldc_w instruction to refer to the constant pool entry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM