dup 0x59

Duplicates the top of the stack.

Opcode
0x59 (89)
Family
Stack manipulation
Operands
none
Length
1 byte
Operand stack
…, value → …, value, value
Can throw
nothing at run time (beyond linking errors)

What it does

Duplicates the top value. Its most common job is object creation: new Foo(x) compiles to new, dup, the arguments and invokespecial <init>. The constructor consumes one copy of the new reference; the other is the result.

It also appears in compound assignments to fields, where the object reference is needed twice (once to read the field and once to write it), and in array initialisers.

How often javac uses it

In JDK 25.0.5's java.base module, dup appears 144,789 times in 4,795 of its 7,391 classes: 7.4% of all instructions, the 2nd most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedInputStream.getInIfOpen, java.io.BufferedOutputStream.<init>, java.io.BufferedReader.<init>, java.io.BufferedReader$1.<init>.

Examples

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

static List make() in Bridge, compiled by javac 25
22return new ArrayList<>();
0new #27// ArrayList
3dup
4invokespecial #29// new ArrayList()

Open in the viewer

int field() in Increments, compiled by javac 25
14return field++;
0aload_0 // this
1dup
2getfield #7// Increments.field : int
8putfield #7// Increments.field : int

Open in the viewer

Related instructions

pop Discards the top stack slot.pop2 Discards the top two stack slots (or one long or double).dup_x1 Duplicates the top value and inserts it two slots down.

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