lookupswitch 0xab

Jumps through sorted match/offset pairs (sparse case values).

Opcode
0xab (171)
Family
Branches and switches
Operands
  • 0–3 bytes: padding to a multiple of 4
  • 4 bytes: default offset
  • 4 bytes: number of pairs
  • 8 × pairs bytes: match and offset pairs, sorted
Length
variable
Operand stack
…, key (int) → …
Can throw
nothing at run time (beyond linking errors)

What it does

Jumps through sorted pairs of (value, offset), or to the default if none matches. Padding after the opcode aligns the pairs to four bytes, and the sort lets the JVM binary-search them.

javac uses it for sparse case values, where a tableswitch would be mostly gaps. A switch on Strings starts with a lookupswitch on the strings' hash codes.

How often javac uses it

In JDK 25.0.5's java.base module, lookupswitch appears 497 times in 250 of its 7,391 classes: 0.03% of all instructions, the 121st most common of 194 opcodes in use. Some of the methods that use it: java.io.DataInputStream.readLine, java.io.FilePermission.getMask, java.io.LineNumberInputStream.read, java.io.LineNumberReader.read.

Examples

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

static int sparse(int code) in Tour, compiled by javac 25
103switch (code) {
0iload_0 // code
1lookupswitch { 1→36, 100→39, 10000→42, default→45 }
104case 1: return 10;
36bipush 10
105case 100: return 20;
39bipush 20
106case 10000: return 30;
42bipush 30
107default: return 0;

Open in the viewer

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

Related instructions

tableswitch Jumps through a table indexed by an int (dense case values).goto Jumps unconditionally.

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