iconst_m1 0x02

Pushes the int constant -1.

Opcode
0x02 (2)
Family
Constants
Operands
none
Length
1 byte
Operand stack
… → …, -1 (int)
Can throw
nothing at run time (beyond linking errors)
Spec name
iconst_<i>

What it does

Pushes the int -1. Only -1 to 5 have one-byte instructions; any other int constant needs bipush (-128 to 127), sipush (-32768 to 32767) or ldc of an Integer constant.

javac also uses iconst_0 and iconst_1 for false and true: booleans are ints in bytecode. It picks the shortest encoding for every int constant, which is why these are among the most common instructions.

How often javac uses it

In JDK 25.0.5's java.base module, iconst_m1 appears 3,740 times in 912 of its 7,391 classes: 0.19% of all instructions, the 67th most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.<init>, java.io.BufferedReader.<init>, java.io.ByteArrayInputStream.read, 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; iconst_m1 is highlighted.

static int oldStyle(Object o) in InstanceofPattern, compiled by javac 25
12if (o instanceof String) {
0aload_0 // o
1instanceof #7// String
13String s = (String) o;
7aload_0 // o
8checkcast #7// String
11astore_1 // s
14return s.length();
12aload_1 // s
13invokevirtual #13// String.length() : int
16return -1;

Open in the viewer

static int dense(int n) in Switches, compiled by javac 25
9switch (n) {
0iload_0 // n
1tableswitch { 0→32, 1→35, 2→38, 3→41, default→44 }
10case 0: return 10;
32bipush 10
11case 1: return 11;
35bipush 11
12case 2: return 12;
38bipush 12
13case 3: return 13;
41bipush 13
14default: return -1;

Open in the viewer

Related instructions

bipush Pushes a byte, sign-extended to int.sipush Pushes a short, sign-extended to int.ldc Pushes a constant from the constant pool (index in one byte).

Specification: JVMS §6.5, iconst_<i> (Java SE 25).