new 0xbb

Allocates an object (uninitialised until its constructor runs).

Opcode
0xbb (187)
Family
Objects and types
Operands
  • 2 bytes: constant pool index
Length
3 bytes
Operand stack
… → …, objectref (uninitialised)
Can throw
InstantiationError

What it does

Allocates an object of a class, with every field zero, and pushes a reference to it. The object cannot be used until a constructor runs: the verifier tracks it as "uninitialised" until invokespecial <init> consumes it.

That is why new Foo(x) is always new, dup, the arguments, then invokespecial: one copy for the constructor, one for the result. The first new of a class initialises it.

How often javac uses it

In JDK 25.0.5's java.base module, new appears 33,545 times in 4,295 of its 7,391 classes: 1.7% of all instructions, the 20th 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.hasNext.

Examples

From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; new 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

public Sync() in Sync, compiled by javac 25
2public class Sync {
0aload_0 // this
1invokespecial #1// new Object()
3private final Object lock = new Object();
4aload_0 // this
5new #2// Object
8dup
9invokespecial #1// new Object()
12putfield #7// Sync.lock : Object

Open in the viewer

Related instructions

dup Duplicates the top of the stack.invokespecial Calls a constructor, a private method or a superclass method directly.anewarray Pops a length; pushes a new array of references.

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