astore 0x3a

Pops a reference into local variable (operand).

Opcode
0x3a (58)
Family
Stores
Operands
  • 1 byte: local variable index
Length
2 bytes
Operand stack
…, value (reference) → …
Can throw
nothing at run time (beyond linking errors)

What it does

Pops a reference into the local variable named by its operand.

Slots 0 to 3 have one-byte forms (astore_0 to astore_3); javac uses this form for slots 4 to 255, and wide for slots above 255.

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 appears 26,719 times in 2,172 of its 7,391 classes: 1.4% of all instructions, the 22nd most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.read, java.io.BufferedReader.read, java.io.BufferedWriter.write, java.io.ByteArrayInputStream.transferTo.

Examples

From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; astore is highlighted.

static int guarded(int[] a, int i) in Switches, compiled by javac 25
… 12 instructions before
65System.out.println("done");
17getstatic #67// System.out : PrintStream
20ldc #73// "done"
22invokevirtual #75// PrintStream.println(String) : void
63return -1;
65System.out.println("done");
27astore 4catch any, any, any
29getstatic #67// System.out : PrintStream
32ldc #73// "done"
34invokevirtual #75// PrintStream.println(String) : void
66}
37aload 4

Open in the viewer

static int arrays(int n) in Tour, compiled by javac 25
… 6 instructions before
60char[] c = new char[n];
8iload_0 // n
9newarray char
11astore_3 // c
61short[] s = new short[n];
12iload_0 // n
13newarray short
15astore 4// s
62int[] i = new int[n];
17iload_0 // n
18newarray int
20astore 5// i
63long[] l = new long[n];
22iload_0 // n
23newarray long
25astore 6// l
… 108 instructions after

Open in the viewer

Related instructions

aload Pushes the reference in local variable (operand).astore_0 Pops a reference into local variable 0.wide Makes the next instruction take a two-byte local index (and a two-byte iinc constant).

Specification: JVMS §6.5, astore (Java SE 25).