The Byte-Level Anatomy of JVM Instructions: Opcodes, Operands, and Constant Pool References
Understanding Bytecode Instructions (4): What's Inside an Instruction
In the previous three articles, we used javap to inspect bytecode instructions: iload_1, iadd, iinc...
- Understanding Bytecode Instructions (1): What's Inside a Frame
- Understanding Bytecode Instructions (2): What's Inside an Empty Loop
- Understanding Bytecode Instructions (3): What's Inside a Single Addition
But a class file stores bytes. How many bytes does one instruction occupy? Where are operands kept? The third article ended with two questions: what do ldc, ldc_w, and ldc2_w each fetch, and why do 100 and 1000000 use different instructions? This article breaks instructions down to the byte level and answers these questions together.
In the following, byte strings without the
0xprefix are hexadecimal;javap's offset column is simply the index into the byte array.
Composition of an Instruction: opcode + operands
The opening of Section 2.11 of the spec states:
A Java Virtual Machine instruction consists of a one-byte opcode specifying the operation to be performed, followed by zero or more operands supplying arguments or data that are used by the operation. Many instructions have no operands and consist only of an opcode.
The first byte of each instruction is the opcode, and operands (if any) follow immediately. The spec then gives the JVM interpreter's main loop:
Ignoring exceptions, the inner loop of a Java Virtual Machine interpreter is effectively
do { atomically calculate pc and fetch opcode at pc; if (operands) fetch operands; execute the action for the opcode; } while (there is more to do);
This can be represented as the following flowchart:
The flowchart's code is in the file interpreter_loop.puml.
pc starts at 0, fetches a one-byte opcode; if the instruction has operands, it fetches them; executes; pc points to the next instruction, and so on. The first three articles looked at the "execute" step; this one examines the first two steps: how opcode and operands are extracted from the byte stream.
The opcode is only 1 byte, a tradeoff the spec makes between compactness and performance: 1 byte has 8 bits, encoding at most 256 instructions, thus limiting the instruction set size. Many instructions are representable with just one byte; the cost is that immediate values exceeding one byte must be assembled from bytes at runtime (Section 2.11).
Section 2.11 also specifies: bytecode is aligned only on single-byte boundaries, the only exceptions being tableswitch and lookupswitch, whose partial operands are padded to a 4-byte boundary. We'll defer why this exception exists to the article on control flow.
Five Methods, Five Operand Forms
We'll use a set of methods as observation targets, covering common operand forms:
class ShowBytes {
int add(int a, int b) { return a + b; }
int addTen(int i) { return i + 10; }
int addThousand(int i) { return i + 1000; }
void inc(int i) { i++; }
int million() { return 1000000; }
}
The byte forms of the five methods are listed below; we'll expand on each:
| Method | Key Instruction | Corresponding Bytes in Method Body | Operand Form |
|---|---|---|---|
add |
iadd |
1b 1c 60 ac |
no operands |
addTen |
bipush 10 |
1b 10 0a 60 ac |
1-byte immediate value |
addThousand |
sipush 1000 |
1b 11 03 e8 60 ac |
2-byte immediate (big-endian) |
inc |
iinc 1, 1 |
84 01 01 b1 |
two operands: slot index + increment |
million |
ldc #7 |
12 07 ac |
1-byte constant pool index |
Zero Operands: the iadd Instruction
Let's look at the add method:
int add(int, int);
descriptor: (II)I
flags: (0x0000)
Code:
stack=2, locals=3, args_size=3
0: iload_1
1: iload_2
2: iadd
3: ireturn
The four instructions have offsets 0, 1, 2, 3; each occupies 1 byte. The method body of add is exactly the four bytes 1b 1c 60 ac:
All four instructions have no operands; the opcode alone is the entire instruction.
Here's a fact that's easy to gloss over: the 1 in iload_1 is not an operand byte but encoded in the opcode; 0x1B already expresses both "load int" and "load slot 1". The JVM provides a single-byte short form for the commonly used slot 0 through slot 3:
Beyond 3, you must use iload plus an operand byte, making it 2 bytes. The same action with two byte lengths is the direct consequence of the "one-byte opcode" tradeoff; later useManyNumeric will show both lstore_3 and lstore 5 side by side as a comparison.
javap doesn't show these bytes; the accompanying demo's byte-stream panel adds that layer:
Each cell is a byte: the cell's top-left corner shows the offset, the middle shows the hexadecimal value, and the bottom shows the role that byte plays in the instruction. At this instant pc = 0, and the boundaries of the four instructions are drawn with brackets; offsets 0, 1, 2, 3 in the javap column correspond exactly to the instruction groups in the figure.
1-Byte Immediate Value: the bipush Instruction
The addTen method introduces an instruction with an operand:
int addTen(int);
descriptor: (I)I
flags: (0x0000)
Code:
stack=2, locals=2, args_size=2
0: iload_1
1: bipush 10
3: iadd
4: ireturn
The method body is 5 bytes: 1b 10 0a 60 ac. The offset of bipush is 1, but the offset of iadd is 3; bipush itself takes two bytes: opcode = 0x10, plus one operand byte 0x0a, which is decimal 10. When bipush is fetched, the byte-stream panel looks like this:
The opcode cell is blue; the operand cell has an amber border; the "fetch operands" step is exactly this. The previous article noted that the b in bipush refers to the immediate value's width (8 bits), not the type of the value on the stack: the pushed value is still an int. In this article, we see that immediate value in the byte stream: 0a.
2-Byte Immediate Value: the sipush Instruction
1000 is larger; a signed 8-bit value can hold at most 127, so bipush can't handle it. addThousand uses sipush:
int addThousand(int);
descriptor: (I)I
flags: (0x0000)
Code:
stack=2, locals=2, args_size=2
0: iload_1
1: sipush 1000
4: iadd
5: ireturn
The method body is 6 bytes: 1b 11 03 e8 60 ac. The opcode = 0x11 of sipush is followed by two operand bytes 03 and e8. How do these two bytes become 1000? The spec gives the rule:
If an operand is more than one byte in size, then it is stored in big-endian order - high-order byte first. For example, an unsigned 16-bit index into the local variables is stored as two unsigned bytes, byte1 and byte2, such that its value is (byte1 << 8) | byte2.
High byte first: (0x03 << 8) | 0xe8 = 1000. All multi-byte operands follow this order; that's big-endian. sipush fetches 16 bits, interprets them as a signed value, then sign-extends to an int and pushes it; the handling is the same as bipush, just double the width, so the range is -32768~32767. When sipush is fetched, both bytes light up simultaneously:
The hi and lo cells' roles are clear: 0x03 is the high byte, 0xe8 is the low byte; assembly happens during the operand-fetch step.
Two Operands: the iinc Instruction
The third article mentioned that iinc is an exception among arithmetic instructions: it doesn't go through the operand stack. Now we can see its byte form:
void inc(int);
descriptor: (I)V
flags: (0x0000)
Code:
stack=0, locals=2, args_size=2
0: iinc 1, 1
3: return
The method body is 4 bytes: 84 01 01 b1. The opcode = 0x84 of iinc is followed by two operand bytes, which javap displays as 1, 1: the first 1 is the slot number in the local variable table, the second 1 is the constant to add. Note the type of the latter: it's a signed 1-byte value, range -128~127, the same width as bipush's immediate. i++ is compressed into a single instruction: which slot to increment and by how much, all written in the bytes, no need to touch the operand stack:
In the figure above, the index and const cells in the byte-stream panel are exactly the two 1s on either side of the comma in javap's listing:
In the demo, i defaults to 1; I set the input parameter i to 42. The frame panel above shows the effect of this instruction: slot 1 changes from 42 to 43, and the operand stack is empty throughout; stack=0. In fact, all methods with parameters can specify their own argument values (see "Accompanying Demo" at the end).
Constant Pool Index: 100 vs 1000000
Operands that can be placed in the instruction are limited: bipush carries 1 byte, sipush carries 2. Larger numbers won't fit, so they need somewhere else to live; that's the constant pool. The opening of Section 3.4 (Accessing the Run-Time Constant Pool) says:
Many numeric constants, as well as objects, fields, and methods, are accessed via the run-time constant pool of the current class. Object access is considered later (§3.8). Data of types
int,long,float, anddouble, as well as references to instances of classString, are managed using the ldc, ldc_w, and ldc2_w instructions.
The following example comes from Section 3.4 of the spec; it's not in the demo's ShowBytes; it shows all five constant types at once:
void useManyNumeric() {
int i = 100;
int j = 1000000;
long l1 = 1;
long l2 = 0xffffffff;
double d = 2.2;
}
Disassembled with javap -v:
void useManyNumeric();
descriptor: ()V
flags: (0x0000)
Code:
stack=2, locals=9, args_size=1
0: bipush 100
2: istore_1
3: ldc #7 // int 1000000
5: istore_2
6: lconst_1
7: lstore_3
8: ldc2_w #8 // long -1l
11: lstore 5
13: ldc2_w #10 // double 2.2d
16: dstore 7
18: return
Back to the byte-length question: lstore_3 at offset 7 is 1 byte, with slot 3 already encoded in the opcode; lstore 5 at offset 11 takes 2 bytes, with slot 5 in a separate operand byte, so the next ldc2_w is at offset 13 rather than 12. The same action, two different byte lengths; that's the "one-byte opcode" tradeoff in real code.
Five assignments, four ways to fetch constants:
int i = 100:bipush 100, the value fits in 1 byte, written directly in the instruction.int j = 1000000:ldc #7: 1000000 exceeds the 16-bit range ofsipush, so the instruction only holds a constant pool index#7; the value resides in the pool.long l1 = 1:lconst_1, small enough that it doesn't even need the constant pool.long l2 = 0xffffffff:ldc2_w #8.javapshowslong -1l;0xffffffffas anintliteral is-1, assigned tolongit's still-1, and the constant pool stores-1L.double d = 2.2:ldc2_w #10.
The constant pool entries used by this class:
#7 = Integer 1000000
#8 = Long -1l
#10 = Double 2.2d
Note that #9 is skipped: Long and Double each occupy two entries in the constant pool, so after #8, the next entry is #10. Here #7 / #8 / #10 are the numbers for this class's example; the spec's 3.4 example uses #1 / #6 / #8; the numbers may vary depending on the class file, but that doesn't affect our understanding of bytecode instructions.
ldc has the same byte structure as bipush: opcode 12 plus one operand byte 07, except the operand's meaning here is not a value but "the 7th entry in the constant pool":
The demo's million() also starts with ldc #7, followed by ireturn (bytes 12 07 ac); after fetching the index, the constant pool panel on the right highlights #7:
The questions from the third article are now fully answered: 100 and 1000000 use different instructions not because of different types (both are int) but because of different widths: values that fit in an immediate go directly in the instruction (bipush 1 byte, sipush 2 bytes), while values that don't fit go into the constant pool. The constant pool entry numbers depend on the specific class file; different compilers may produce different results. But how many bytes an instruction occupies is fixed.
ldc, ldc_w, and ldc2_w: Index Width and Value Width
The difference among the three is only in two aspects; the spec is clear:
The ldc and ldc_w instructions are used to access values in the run-time constant pool (including instances of class
String) of types other thandoubleandlong. The ldc_w instruction is used in place of ldc only when there is a large number of run-time constant pool items and a larger index is needed to access an item. The ldc2_w instruction is used to access all values of typesdoubleandlong; there is no non-wide variant.
Two dimensions each handle one thing:
- Type of value:
doubleandlongoccupy 2 slots (the width discussed in the third article), so they can only go throughldc2_w; all other types useldcorldc_w. - Width of index:
ldc's index is 1 byte, addressing1~255(0 is not a valid constant pool index); when the index exceeds 255, you must switch toldc_w, whose operand is 2 bytes.
There's another sentence in the quote worth noting: ldc2_w has no non-wide variant; there is no 1-byte-index ldc2. Its 2 refers to the value occupying two slots, and w means the index is also necessarily 2 bytes; while ldc_w's w only changes the index width, the value on the stack is still a 1-slot int, float, or reference. Don't mix up the two "widths".
This article's demo only illustrates ldc: ldc_w is identical except the index occupies 2 bytes; the steps of fetching the operand, looking up the pool, and pushing the value are the same. ldc2_w just adds the dimension of "value occupying two slots".
A quick look at the byte-level differences:
| Instruction | opcode | Operand bytes | Meaning |
|---|---|---|---|
ldc #7 |
12 |
07 (1 byte) |
constant pool entry 7, value occupies 1 slot |
ldc_w #300 |
13 |
01 2c (2 bytes) |
constant pool entry 300, value still 1 slot |
ldc2_w #8 |
14 |
00 08 (2 bytes) |
constant pool entry 8, value is long/double, occupies 2 slots |
01 2c is the big-endian representation of 300. To make javap actually output ldc_w, you need to stuff more than 255 entries into the class's constant pool (indexing starts from 1, e.g., hundreds of distinct strings); to make ldc2_w appear, writing long x = 1L << 40; is enough; the value exceeds int range, so it must go into the constant pool.
Accompanying Demo
The accompanying InstructionDemo.java is a Swing window with a dropdown to switch among the five methods: add (zero operands), addTen (bipush), addThousand (sipush), inc (iinc), million (ldc). On the left are the source and decoded instructions; on the top right is the byte-stream panel; on the bottom right are the local variable table, operand stack, and constant pool. The demo covers only these five forms; the differences of ldc_w / ldc2_w are only in index and value width, so no separate example is given. After downloading, run (requires JDK 16+ due to the use of record; this article uses JDK 21):
javac InstructionDemo.java
java InstructionDemo
Switch methods at the top of the window; the second line shows editable argument input fields based on the current method's signature: add has a and b, addTen / addThousand / inc each have an i, and million shows none since it has no parameters. Fill in integers in the fields and click Apply (or press Enter in the field); the demo rebuilds the execution process with the new argument values: the local variable table, operand stack, description text, and return value all change, while the bytecode in the byte-stream panel remains unchanged. That's a visualization of the fact that "parameters live in slots of the local variable table, and instructions are fixed bytes". Each method's input value is saved separately; switching away and back doesn't lose it.
The control buttons work as before: Step ▶ executes one instruction, Run automatically plays the remaining instructions, Reset returns to pc = 0.
Color coding in the byte-stream panel: the blue cell is the opcode fetched at this step, the amber-bordered cell is the operand byte fetched at this step, light blue cells are already-fetched bytes; the pc arrow points to the next byte to fetch. The constant pool panel is an excerpt of the real constant pool; a class's constant pool also has many entries like class names, method names, etc.; these are marked with "略" (omitted).
The bytes and constant pool indexes are taken from real class files: each method's instruction listing and #7 match the output of javap -v. The demo doesn't parse class files; instead, it preloads verified bytes into a simulated interpreter loop, which is exactly the do-while from the opening of 2.11.
Verify These Bytes Yourself
The byte strings in this article aren't hand-computed; readers can reproduce them. Save the ShowBytes above as ShowBytes.java, keeping the methods package-visible (don't add public), otherwise the flags in the listing will change from 0x0000 to 0x0001 and won't match the article. Then:
javac -g ShowBytes.java
javap -p -c -v ShowBytes
-p is essential; ShowBytes methods are package-visible, and javap only lists public / protected members by default. This command gives mnemonics and offsets (e.g., 1: bipush 10, 3: iadd), but it does not print the hexadecimal bytes. To see the raw bytes, use xxd (macOS / Linux) or hexdump -C:
xxd ShowBytes.class
Locate the method: in a class file, each method's Code attribute records max_stack, max_locals, code_length in order; the code_length bytes immediately following are the method body; the offsets 0, 1, 3 in javap output are indexes into this byte array. For addTen, find 1b 10 0a 60 ac in the xxd output, compare it with javap's 1: bipush 10, and you'll see how "opcode + operands" lands in a sequence of bytes.
An easier route is to use ASM's ClassReader to directly read the code array and decode it byte by byte.
Byte Reference Table
| Bytes | Meaning | Source |
|---|---|---|
1b 1c 60 ac |
Four single-byte instructions | Entire bytes of add(int, int) |
1b 10 0a 60 ac |
bipush with 1-byte immediate |
Entire bytes of addTen(int) |
03 e8 |
16-bit immediate, big-endian | sipush 1000, `(0x03 << 8) |
84 01 01 b1 |
Two operands: slot and increment | Entire bytes of inc(int) |
12 07 ac |
ldc with 1-byte constant pool index |
Entire bytes of million(), #7 points to Integer 1000000 |
Exercise: Does 200 Go with bipush or sipush
Add int half() { return 200; } to ShowBytes and run javap. Does it use bipush or sipush? Why?
Here's the answer (try it yourself first):
int half();
descriptor: ()I
flags: (0x0000)
Code:
stack=1, locals=1, args_size=1
0: sipush 200
3: ireturn
bipush's operand is a signed 8-bit value, max 127; 200 doesn't fit, so it must use the 16-bit sipush. A boundary case: 128 uses sipush, but -128 still uses bipush; the criterion is "can it fit in a signed 1-byte value", not the absolute magnitude. The constant in iinc has the same 1-byte boundary; if you change i++ to i += 200, iinc can't hold it and you'd have to split into iload / sipush / iadd / istore four steps.
The exercise in the third article was about type determining instruction (byte parameter uses int instructions); this one is about width determining instruction. Two determining factors, and the previous article and this one each covered one.
Summary
- One instruction = 1-byte opcode + zero or more operands: the interpreter loop is "fetch opcode, fetch operands, execute", with pc moving along the byte stream.
- Operand bytes fall into three categories: no operands, where information is already encoded in the opcode (
iload_1,iadd); immediate values, where the value itself follows the opcode (bipush,sipush,iinc's constant); indexes, which point elsewhere (iinc's local variable slot, constant pool indexesldc/ldc_w/ldc2_w, and branch instruction offsets). - Multi-byte operands are always big-endian: high byte first,
(byte1 << 8) | byte2. 100usesbipush,1000000usesldc: values that fit in an immediate go in the instruction, values that don't go into the constant pool;long/doublecan only go throughldc2_w.- Indexes above 255 use
ldc_w: the operand changes from 1 byte to 2 bytes, nothing more.
The next article will look at control flow: what instructions if and switch compile to, and how tableswitch and lookupswitch are chosen. The "only alignment exception" left by 2.11 will be settled then. Would you like to see tableswitch's padding bytes first, or if's condition inversion? Leave a comment in the discussion.
Top 2 of 3 from juejin.cn, machine-translated. The original thread is authoritative.
Okay
Thanks for the recognition
It's easier to understand if index width and value width are separated.