I have been learning about the java class format and i was just wondering why sometimes an opcode with a constant pool entry following takes up two bytes in the class file such as with InvokeStatic, but then op-codes such as ldc with an integer index take up only one byte in the class file. Is there any pattern behind this, i am asking this because i am writing a simple byte-code manipulation library and i would like to be able to know weather to write a constant pool index as a byte or a short without hard-coding every single command into the library.
Asked
Active
Viewed 45 times
0
-
`xxx_w` is wider than `xxx`. – johnchen902 Jul 02 '13 at 10:46
-
For example `ldc_w` takes up two bytes while `ldc` takes up one. – johnchen902 Jul 02 '13 at 11:41
1 Answers
1
Yes there is a pattern: every instruction except ldc
takes a two byte index.
Presumably the designers of the bytecode format deciding that loading constants was such a common task that they should provide a shorter instruction for it.

Antimony
- 37,781
- 10
- 100
- 107
-
-
@Popgalop You'd have to ask the designers of Java for that. Probably they just didn't think it'd be worth it. – Antimony Jul 09 '13 at 03:02