iconst_4 0x07

Pushes the int constant 4.

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

What it does

Pushes the int 4. 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_4 appears 5,561 times in 962 of its 7,391 classes: 0.29% of all instructions, the 59th most common of 194 opcodes in use. Some of the methods that use it: java.io.Console.isStderrTty, java.io.DataInputStream.readInt, java.io.DataOutputStream.writeInt, java.io.File.canRead.

Examples

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

static int price(EnumSwitch$Size size) in EnumSwitch, compiled by javac 25
9switch (size) {
0aload_0 // size
1invokevirtual #7// EnumSwitch$Size.ordinal() : int
4lookupswitch { 0→32, 1→34, default→36 }
11return 3;
13return 4;
15return 5;

Open in the viewer

static int check(int n) in Constants, compiled by javac 25
9if (n > LIMIT) {
0iload_0 // n
1sipush 1000
10System.out.println(NAME + " exceeded");
7getstatic #9// System.out : PrintStream
10ldc #15// "limit exceeded"
12invokevirtual #17// PrintStream.println(String) : void
11return COMPUTED;
15getstatic #23// Constants.COMPUTED : int
13return (int) (BIG >> 38);

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).