astore_2 0x4d
Pops a reference into local variable 2.
- Opcode
0x4d(77)- Family
- Stores
- Operands
- none
- Length
- 1 byte
- Operand stack
…, value (reference) → …- Can throw
- nothing at run time (beyond linking errors)
- Spec name
astore_<n>
What it does
Pops a reference into local variable 2.
Slots 0 to 3 each have a one-byte instruction; the general astore takes a one-byte slot number, and wide extends that to two bytes.
astore can also store a return address left by jsr, which only matters for very old class files.
How often javac uses it
In JDK 25.0.5's java.base module, astore_2 appears 8,282 times in 2,195 of its 7,391 classes: 0.42% of all instructions, the 48th most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.getBufIfOpen, java.io.BufferedReader.read, java.io.BufferedWriter.flushBuffer, java.io.CharArrayReader.read.
Examples
From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; astore_2 is highlighted.
static String repeat(String s, int times) in Concat, compiled by javac 25String out = "";for (int i = 0; i < times; i++) {out += s;for (int i = 0; i < times; i++) {return out;static int sum(List values) in Boxing, compiled by javac 25Related instructions
aload_2 Pushes the reference in local variable 2.astore Pops a reference into local variable (operand).wide Makes the next instruction take a two-byte local index (and a two-byte iinc constant).
Specification: JVMS §6.5, astore_<n> (Java SE 25).